github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/plugins/trigger/ic_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 trigger 18 19 import ( 20 "testing" 21 22 "github.com/sirupsen/logrus" 23 24 "k8s.io/test-infra/prow/config" 25 "k8s.io/test-infra/prow/github" 26 "k8s.io/test-infra/prow/github/fakegithub" 27 "k8s.io/test-infra/prow/kube" 28 ) 29 30 type fkc struct { 31 started []string 32 } 33 34 func (c *fkc) CreateProwJob(pj kube.ProwJob) (kube.ProwJob, error) { 35 c.started = append(c.started, pj.Spec.Context) 36 return pj, nil 37 } 38 39 func TestHandleIssueComment(t *testing.T) { 40 var testcases = []struct { 41 name string 42 43 Author string 44 Body string 45 State string 46 IsPR bool 47 Branch string 48 ShouldBuild bool 49 HasOkToTest bool 50 IsOkToTest bool 51 StartsExactly string 52 Presubmits map[string][]config.Presubmit 53 IssueLabels []github.Label 54 }{ 55 { 56 name: "Not a PR.", 57 58 Author: "t", 59 Body: "/ok-to-test", 60 State: "open", 61 IsPR: false, 62 ShouldBuild: false, 63 }, 64 { 65 name: "Closed PR.", 66 67 Author: "t", 68 Body: "/ok-to-test", 69 State: "closed", 70 IsPR: true, 71 ShouldBuild: false, 72 }, 73 { 74 name: "Comment by a bot.", 75 76 Author: "k8s-bot", 77 Body: "/ok-to-test", 78 State: "open", 79 IsPR: true, 80 ShouldBuild: false, 81 }, 82 { 83 name: "Non-trusted member's ok to test.", 84 85 Author: "u", 86 Body: "/ok-to-test", 87 State: "open", 88 IsPR: true, 89 ShouldBuild: false, 90 }, 91 { 92 name: `Non-trusted member after "/ok-to-test".`, 93 94 Author: "u", 95 Body: "/test all", 96 State: "open", 97 IsPR: true, 98 HasOkToTest: true, 99 ShouldBuild: true, 100 }, 101 { 102 name: "Trusted member's ok to test", 103 104 Author: "t", 105 Body: "looks great, thanks!\n/ok-to-test", 106 State: "open", 107 IsPR: true, 108 ShouldBuild: true, 109 }, 110 { 111 name: "Trusted member's ok to test, trailing space.", 112 113 Author: "t", 114 Body: "looks great, thanks!\n/ok-to-test \r", 115 State: "open", 116 IsPR: true, 117 ShouldBuild: true, 118 }, 119 { 120 name: "Trusted member's not ok to test.", 121 122 Author: "t", 123 Body: "not /ok-to-test", 124 State: "open", 125 IsPR: true, 126 ShouldBuild: false, 127 }, 128 { 129 name: "Trusted member's test this.", 130 131 Author: "t", 132 Body: "/test all", 133 State: "open", 134 IsPR: true, 135 ShouldBuild: true, 136 }, 137 { 138 name: "Wrong branch.", 139 140 Author: "t", 141 Body: "/test", 142 State: "open", 143 IsPR: true, 144 Branch: "other", 145 ShouldBuild: false, 146 }, 147 { 148 name: "Retest with one running and one failed", 149 150 Author: "t", 151 Body: "/retest", 152 State: "open", 153 IsPR: true, 154 ShouldBuild: true, 155 StartsExactly: "pull-jib", 156 }, 157 { 158 name: "Retest with one running and one failed, trailing space.", 159 160 Author: "t", 161 Body: "/retest \r", 162 State: "open", 163 IsPR: true, 164 ShouldBuild: true, 165 StartsExactly: "pull-jib", 166 }, 167 { 168 name: "needs-ok-to-test label is removed when no presubmit runs by default", 169 170 Author: "t", 171 Body: "/ok-to-test", 172 State: "open", 173 IsPR: true, 174 IsOkToTest: true, 175 ShouldBuild: false, 176 Presubmits: map[string][]config.Presubmit{ 177 "org/repo": { 178 { 179 Name: "job", 180 AlwaysRun: false, 181 Context: "pull-job", 182 Trigger: `/test all`, 183 }, 184 { 185 Name: "jib", 186 AlwaysRun: false, 187 Context: "pull-jib", 188 Trigger: `/test jib`, 189 }, 190 }, 191 }, 192 IssueLabels: []github.Label{{Name: "needs-ok-to-test"}}, 193 }, 194 } 195 for _, tc := range testcases { 196 t.Logf("running scenarion %q", tc.name) 197 if tc.Branch == "" { 198 tc.Branch = "master" 199 } 200 g := &fakegithub.FakeClient{ 201 IssueComments: map[int][]github.IssueComment{}, 202 OrgMembers: []string{"t"}, 203 PullRequests: map[int]*github.PullRequest{ 204 0: { 205 Number: 0, 206 Head: github.PullRequestBranch{ 207 SHA: "cafe", 208 }, 209 Base: github.PullRequestBranch{ 210 Ref: tc.Branch, 211 Repo: github.Repo{ 212 Name: "repo", 213 }, 214 }, 215 }, 216 }, 217 CombinedStatuses: map[string]*github.CombinedStatus{ 218 "cafe": { 219 Statuses: []github.Status{ 220 {State: "pending", Context: "pull-job"}, 221 {State: "failure", Context: "pull-jib"}, 222 }, 223 }, 224 }, 225 } 226 kc := &fkc{} 227 c := client{ 228 GitHubClient: g, 229 KubeClient: kc, 230 Config: &config.Config{}, 231 Logger: logrus.WithField("plugin", pluginName), 232 } 233 presubmits := tc.Presubmits 234 if presubmits == nil { 235 presubmits = map[string][]config.Presubmit{ 236 "org/repo": { 237 { 238 Name: "job", 239 AlwaysRun: true, 240 Context: "pull-job", 241 Trigger: `/test all`, 242 Brancher: config.Brancher{Branches: []string{"master"}}, 243 }, 244 { 245 Name: "jib", 246 AlwaysRun: false, 247 Context: "pull-jib", 248 Trigger: `/test jib`, 249 }, 250 }, 251 } 252 } 253 c.Config.SetPresubmits(presubmits) 254 255 var pr *struct{} 256 if tc.IsPR { 257 pr = &struct{}{} 258 } 259 if tc.HasOkToTest { 260 g.IssueComments[0] = []github.IssueComment{{ 261 Body: "/ok-to-test", 262 User: github.User{Login: "t"}, 263 }} 264 } 265 event := github.IssueCommentEvent{ 266 Action: github.IssueCommentActionCreated, 267 Repo: github.Repo{ 268 Name: "repo", 269 FullName: "org/repo", 270 }, 271 Comment: github.IssueComment{ 272 Body: tc.Body, 273 User: github.User{Login: tc.Author}, 274 }, 275 Issue: github.Issue{ 276 PullRequest: pr, 277 State: tc.State, 278 }, 279 } 280 if len(tc.IssueLabels) > 0 { 281 event.Issue.Labels = tc.IssueLabels 282 } 283 284 if err := handleIC(c, "kubernetes", event); err != nil { 285 t.Fatalf("Didn't expect error: %s", err) 286 } 287 if len(kc.started) > 0 && !tc.ShouldBuild { 288 t.Errorf("Built but should not have: %+v", tc) 289 } else if len(kc.started) == 0 && tc.ShouldBuild { 290 t.Errorf("Not built but should have: %+v", tc) 291 } 292 if tc.StartsExactly != "" && (len(kc.started) != 1 || kc.started[0] != tc.StartsExactly) { 293 t.Errorf("Didn't build expected context %v, instead built %v", tc.StartsExactly, kc.started) 294 } 295 if tc.IsOkToTest { 296 if len(g.LabelsRemoved) != 1 { 297 t.Errorf("expected a label to be removed") 298 continue 299 } 300 if g.LabelsRemoved[0] != "/repo#0:needs-ok-to-test" { 301 t.Errorf("expected %q to be removed, got %q", "/repo#0:needs-ok-to-test", g.LabelsRemoved[0]) 302 } 303 } 304 } 305 }