github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/sig-mention-handler_test.go (about) 1 /* 2 Copyright 2017 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 mungers 18 19 import ( 20 "fmt" 21 "net/http" 22 "reflect" 23 "runtime" 24 "testing" 25 26 githubapi "github.com/google/go-github/github" 27 "k8s.io/test-infra/mungegithub/github" 28 github_test "k8s.io/test-infra/mungegithub/github/testing" 29 ) 30 31 const ( 32 helpWanted = "help-wanted" 33 open = "open" 34 sigApps = "sig/apps" 35 committeeSteering = "committee/steering" 36 wgContainerIdentity = "wg/container-identity" 37 username = "Ali" 38 ) 39 40 func TestSigMentionHandler(t *testing.T) { 41 runtime.GOMAXPROCS(runtime.NumCPU()) 42 43 tests := []struct { 44 name string 45 issue *githubapi.Issue 46 expected []githubapi.Label 47 }{ 48 { 49 name: "ignore PRs", 50 issue: &githubapi.Issue{ 51 State: githubapi.String(open), 52 Labels: []githubapi.Label{{Name: githubapi.String(helpWanted)}}, 53 PullRequestLinks: &githubapi.PullRequestLinks{}, 54 Assignee: &githubapi.User{Login: githubapi.String(username)}, 55 Number: intPtr(1), 56 }, 57 expected: []githubapi.Label{{Name: githubapi.String(helpWanted)}}, 58 }, 59 { 60 name: "issue is null", 61 issue: nil, 62 expected: nil, 63 }, 64 { 65 name: "issue state is null", 66 issue: &githubapi.Issue{ 67 State: nil, 68 Labels: []githubapi.Label{{Name: githubapi.String(helpWanted)}}, 69 PullRequestLinks: nil, 70 Assignee: &githubapi.User{Login: githubapi.String(username)}, 71 Number: intPtr(1), 72 }, 73 expected: []githubapi.Label{{Name: githubapi.String(helpWanted)}}, 74 }, 75 { 76 name: "issue is closed", 77 issue: &githubapi.Issue{ 78 State: githubapi.String("closed"), 79 Labels: []githubapi.Label{{Name: githubapi.String(helpWanted)}}, 80 PullRequestLinks: nil, 81 Assignee: &githubapi.User{Login: githubapi.String(username)}, 82 Number: intPtr(1), 83 }, 84 expected: []githubapi.Label{{Name: githubapi.String(helpWanted)}}, 85 }, 86 { 87 name: "issue has sig/foo label, no needs-sig label", 88 issue: &githubapi.Issue{ 89 State: githubapi.String(open), 90 Labels: []githubapi.Label{{Name: githubapi.String(helpWanted)}, 91 {Name: githubapi.String(sigApps)}}, 92 PullRequestLinks: nil, 93 Assignee: &githubapi.User{Login: githubapi.String(username)}, 94 Number: intPtr(1), 95 }, 96 expected: []githubapi.Label{{Name: githubapi.String(helpWanted)}, 97 {Name: githubapi.String(sigApps)}}, 98 }, 99 { 100 name: "issue has no sig/foo label, no needs-sig label", 101 issue: &githubapi.Issue{ 102 State: githubapi.String(open), 103 Labels: []githubapi.Label{{Name: githubapi.String(helpWanted)}}, 104 PullRequestLinks: nil, 105 Assignee: &githubapi.User{Login: githubapi.String(username)}, 106 Number: intPtr(1), 107 }, 108 expected: []githubapi.Label{{Name: githubapi.String(helpWanted)}, 109 {Name: githubapi.String(needsSigLabel)}}, 110 }, 111 { 112 name: "issue has needs-sig label, no sig/foo label", 113 issue: &githubapi.Issue{ 114 State: githubapi.String(open), 115 Labels: []githubapi.Label{{Name: githubapi.String(helpWanted)}, 116 {Name: githubapi.String(needsSigLabel)}}, 117 PullRequestLinks: nil, 118 Assignee: &githubapi.User{Login: githubapi.String(username)}, 119 Number: intPtr(1), 120 }, 121 expected: []githubapi.Label{{Name: githubapi.String(helpWanted)}, 122 {Name: githubapi.String(needsSigLabel)}}, 123 }, 124 { 125 name: "issue has both needs-sig label and sig/foo label", 126 issue: &githubapi.Issue{ 127 State: githubapi.String(open), 128 Labels: []githubapi.Label{{Name: githubapi.String(helpWanted)}, 129 {Name: githubapi.String(needsSigLabel)}, 130 {Name: githubapi.String(sigApps)}}, 131 PullRequestLinks: nil, 132 Assignee: &githubapi.User{Login: githubapi.String(username)}, 133 Number: intPtr(1), 134 }, 135 expected: []githubapi.Label{{Name: githubapi.String(helpWanted)}, 136 {Name: githubapi.String(sigApps)}}, 137 }, 138 { 139 name: "issue has committee/foo label, no needs-sig label", 140 issue: &githubapi.Issue{ 141 State: githubapi.String(open), 142 Labels: []githubapi.Label{{Name: githubapi.String(helpWanted)}, 143 {Name: githubapi.String(committeeSteering)}}, 144 PullRequestLinks: nil, 145 Assignee: &githubapi.User{Login: githubapi.String(username)}, 146 Number: intPtr(1), 147 }, 148 expected: []githubapi.Label{{Name: githubapi.String(helpWanted)}, 149 {Name: githubapi.String(committeeSteering)}}, 150 }, 151 { 152 name: "issue has both needs-sig label and committee/foo label", 153 issue: &githubapi.Issue{ 154 State: githubapi.String(open), 155 Labels: []githubapi.Label{{Name: githubapi.String(helpWanted)}, 156 {Name: githubapi.String(needsSigLabel)}, 157 {Name: githubapi.String(committeeSteering)}}, 158 PullRequestLinks: nil, 159 Assignee: &githubapi.User{Login: githubapi.String(username)}, 160 Number: intPtr(1), 161 }, 162 expected: []githubapi.Label{{Name: githubapi.String(helpWanted)}, 163 {Name: githubapi.String(committeeSteering)}}, 164 }, 165 { 166 name: "issue has wg/foo label, no needs-sig label", 167 issue: &githubapi.Issue{ 168 State: githubapi.String(open), 169 Labels: []githubapi.Label{{Name: githubapi.String(helpWanted)}, 170 {Name: githubapi.String(wgContainerIdentity)}}, 171 PullRequestLinks: nil, 172 Assignee: &githubapi.User{Login: githubapi.String(username)}, 173 Number: intPtr(1), 174 }, 175 expected: []githubapi.Label{{Name: githubapi.String(helpWanted)}, 176 {Name: githubapi.String(wgContainerIdentity)}}, 177 }, 178 { 179 name: "issue has both needs-sig label and wg/foo label", 180 issue: &githubapi.Issue{ 181 State: githubapi.String(open), 182 Labels: []githubapi.Label{{Name: githubapi.String(helpWanted)}, 183 {Name: githubapi.String(needsSigLabel)}, 184 {Name: githubapi.String(wgContainerIdentity)}}, 185 PullRequestLinks: nil, 186 Assignee: &githubapi.User{Login: githubapi.String(username)}, 187 Number: intPtr(1), 188 }, 189 expected: []githubapi.Label{{Name: githubapi.String(helpWanted)}, 190 {Name: githubapi.String(wgContainerIdentity)}}, 191 }, 192 } 193 194 for testNum, test := range tests { 195 client, server, mux := github_test.InitServer(t, test.issue, nil, nil, nil, nil, nil, nil) 196 config := &github.Config{ 197 Org: "o", 198 Project: "r", 199 } 200 config.SetClient(client) 201 202 mux.HandleFunc(fmt.Sprintf("/repos/o/r/issues/1/labels/%s", needsSigLabel), func(w http.ResponseWriter, r *http.Request) { 203 w.WriteHeader(http.StatusOK) 204 }) 205 206 obj := &github.MungeObject{} 207 var err error 208 209 if test.issue != nil { 210 obj, err = config.GetObject(*test.issue.Number) 211 212 if err != nil { 213 t.Fatalf("%d: unable to get issue: %v", testNum, *test.issue.Number) 214 } 215 216 obj.Issue = test.issue 217 } 218 219 s := SigMentionHandler{} 220 s.Munge(obj) 221 222 if obj.Issue == nil { 223 if test.expected != nil { 224 t.Errorf("%d:%s: expected: %v, saw: %v", testNum, test.name, test.expected, nil) 225 } else { 226 continue 227 } 228 } 229 230 if !reflect.DeepEqual(test.expected, obj.Issue.Labels) { 231 t.Errorf("%d:%s: expected: %v, saw: %v", testNum, test.name, test.expected, obj.Issue.Labels) 232 } 233 234 server.Close() 235 } 236 }