github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/prow/plugins/trigger/pull-request_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 "encoding/json" 21 "testing" 22 23 "github.com/sirupsen/logrus" 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/labels" 28 "k8s.io/test-infra/prow/plugins" 29 ) 30 31 func TestTrusted(t *testing.T) { 32 const rando = "random-person" 33 const member = "org-member" 34 const sister = "trusted-org-member" 35 const friend = "repo-collaborator" 36 37 var testcases = []struct { 38 name string 39 author string 40 labels []string 41 onlyOrg bool 42 expected bool 43 }{ 44 { 45 name: "trust org member", 46 author: member, 47 labels: []string{}, 48 expected: true, 49 }, 50 { 51 name: "trust member of other trusted org", 52 author: sister, 53 labels: []string{}, 54 expected: true, 55 }, 56 { 57 name: "accept random PR with ok-to-test", 58 author: rando, 59 labels: []string{labels.OkToTest}, 60 expected: true, 61 }, 62 { 63 name: "accept random PR with both labels", 64 author: rando, 65 labels: []string{labels.OkToTest, labels.NeedsOkToTest}, 66 expected: true, 67 }, 68 { 69 name: "reject random PR with needs-ok-to-test", 70 author: rando, 71 labels: []string{labels.NeedsOkToTest}, 72 expected: false, 73 }, 74 { 75 name: "reject random PR with no label", 76 author: rando, 77 labels: []string{}, 78 expected: false, 79 }, 80 } 81 for _, tc := range testcases { 82 t.Run(tc.name, func(t *testing.T) { 83 g := &fakegithub.FakeClient{ 84 OrgMembers: map[string][]string{"kubernetes": {sister}, "kubernetes-incubator": {member, fakegithub.Bot}}, 85 Collaborators: []string{friend}, 86 IssueComments: map[int][]github.IssueComment{}, 87 } 88 trigger := plugins.Trigger{ 89 TrustedOrg: "kubernetes", 90 OnlyOrgMembers: tc.onlyOrg, 91 } 92 var labels []github.Label 93 for _, label := range tc.labels { 94 labels = append(labels, github.Label{ 95 Name: label, 96 }) 97 } 98 _, actual, err := TrustedPullRequest(g, &trigger, tc.author, "kubernetes-incubator", "random-repo", 1, labels) 99 if err != nil { 100 t.Fatalf("Didn't expect error: %s", err) 101 } 102 if actual != tc.expected { 103 t.Errorf("actual result %t != expected %t", actual, tc.expected) 104 } 105 }) 106 } 107 } 108 109 func TestHandlePullRequest(t *testing.T) { 110 var testcases = []struct { 111 name string 112 113 Author string 114 ShouldBuild bool 115 ShouldComment bool 116 HasOkToTest bool 117 prLabel string 118 prChanges bool 119 prAction github.PullRequestEventAction 120 }{ 121 { 122 name: "Trusted user open PR should build", 123 124 Author: "t", 125 ShouldBuild: true, 126 prAction: github.PullRequestActionOpened, 127 }, 128 { 129 name: "Untrusted user open PR should not build and should comment", 130 131 Author: "u", 132 ShouldBuild: false, 133 ShouldComment: true, 134 prAction: github.PullRequestActionOpened, 135 }, 136 { 137 name: "Trusted user reopen PR should build", 138 139 Author: "t", 140 ShouldBuild: true, 141 prAction: github.PullRequestActionReopened, 142 }, 143 { 144 name: "Untrusted user reopen PR with ok-to-test should build", 145 146 Author: "u", 147 ShouldBuild: true, 148 HasOkToTest: true, 149 prAction: github.PullRequestActionReopened, 150 }, 151 { 152 name: "Untrusted user reopen PR without ok-to-test should not build", 153 154 Author: "u", 155 ShouldBuild: false, 156 prAction: github.PullRequestActionReopened, 157 }, 158 { 159 name: "Trusted user edit PR with changes should build", 160 161 Author: "t", 162 ShouldBuild: true, 163 prChanges: true, 164 prAction: github.PullRequestActionEdited, 165 }, 166 { 167 name: "Trusted user edit PR without changes should not build", 168 169 Author: "t", 170 ShouldBuild: false, 171 prAction: github.PullRequestActionEdited, 172 }, 173 { 174 name: "Untrusted user edit PR without changes and without ok-to-test should not build", 175 176 Author: "u", 177 ShouldBuild: false, 178 prAction: github.PullRequestActionEdited, 179 }, 180 { 181 name: "Untrusted user edit PR with changes and without ok-to-test should not build", 182 183 Author: "u", 184 ShouldBuild: false, 185 prChanges: true, 186 prAction: github.PullRequestActionEdited, 187 }, 188 { 189 name: "Untrusted user edit PR without changes and with ok-to-test should not build", 190 191 Author: "u", 192 ShouldBuild: false, 193 HasOkToTest: true, 194 prAction: github.PullRequestActionEdited, 195 }, 196 { 197 name: "Untrusted user edit PR with changes and with ok-to-test should build", 198 199 Author: "u", 200 ShouldBuild: true, 201 HasOkToTest: true, 202 prChanges: true, 203 prAction: github.PullRequestActionEdited, 204 }, 205 { 206 name: "Trusted user sync PR should build", 207 208 Author: "t", 209 ShouldBuild: true, 210 prAction: github.PullRequestActionSynchronize, 211 }, 212 { 213 name: "Untrusted user sync PR without ok-to-test should not build", 214 215 Author: "u", 216 ShouldBuild: false, 217 prAction: github.PullRequestActionSynchronize, 218 }, 219 { 220 name: "Untrusted user sync PR with ok-to-test should build", 221 222 Author: "u", 223 ShouldBuild: true, 224 HasOkToTest: true, 225 prAction: github.PullRequestActionSynchronize, 226 }, 227 { 228 name: "Trusted user labeled PR with lgtm should not build", 229 230 Author: "t", 231 ShouldBuild: false, 232 prAction: github.PullRequestActionLabeled, 233 prLabel: labels.LGTM, 234 }, 235 { 236 name: "Untrusted user labeled PR with lgtm should build", 237 238 Author: "u", 239 ShouldBuild: true, 240 prAction: github.PullRequestActionLabeled, 241 prLabel: labels.LGTM, 242 }, 243 { 244 name: "Untrusted user labeled PR without lgtm should not build", 245 246 Author: "u", 247 ShouldBuild: false, 248 prAction: github.PullRequestActionLabeled, 249 prLabel: "test", 250 }, 251 { 252 name: "Trusted user closed PR should not build", 253 254 Author: "t", 255 ShouldBuild: false, 256 prAction: github.PullRequestActionClosed, 257 }, 258 } 259 for _, tc := range testcases { 260 t.Logf("running scenario %q", tc.name) 261 262 g := &fakegithub.FakeClient{ 263 IssueComments: map[int][]github.IssueComment{}, 264 OrgMembers: map[string][]string{"org": {"t"}}, 265 PullRequests: map[int]*github.PullRequest{ 266 0: { 267 Number: 0, 268 User: github.User{Login: tc.Author}, 269 Base: github.PullRequestBranch{ 270 Ref: "master", 271 Repo: github.Repo{ 272 Owner: github.User{Login: "org"}, 273 Name: "repo", 274 }, 275 }, 276 }, 277 }, 278 } 279 kc := &fkc{} 280 c := Client{ 281 GitHubClient: g, 282 KubeClient: kc, 283 Config: &config.Config{}, 284 Logger: logrus.WithField("plugin", pluginName), 285 } 286 287 presubmits := map[string][]config.Presubmit{ 288 "org/repo": { 289 { 290 JobBase: config.JobBase{ 291 Name: "jib", 292 }, 293 AlwaysRun: true, 294 }, 295 }, 296 } 297 if err := c.Config.SetPresubmits(presubmits); err != nil { 298 t.Fatalf("failed to set presubmits: %v", err) 299 } 300 301 if tc.HasOkToTest { 302 g.IssueLabelsExisting = append(g.IssueLabelsExisting, issueLabels(labels.OkToTest)...) 303 } 304 pr := github.PullRequestEvent{ 305 Action: tc.prAction, 306 Label: github.Label{Name: tc.prLabel}, 307 PullRequest: github.PullRequest{ 308 Number: 0, 309 User: github.User{Login: tc.Author}, 310 Base: github.PullRequestBranch{ 311 Ref: "master", 312 Repo: github.Repo{ 313 Owner: github.User{Login: "org"}, 314 Name: "repo", 315 FullName: "org/repo", 316 }, 317 }, 318 }, 319 } 320 if tc.prChanges { 321 data := []byte(`{"base":{"ref":{"from":"REF"}, "sha":{"from":"SHA"}}}`) 322 pr.Changes = (json.RawMessage)(data) 323 } 324 trigger := plugins.Trigger{ 325 TrustedOrg: "org", 326 OnlyOrgMembers: true, 327 } 328 if err := handlePR(c, &trigger, pr); err != nil { 329 t.Fatalf("Didn't expect error: %s", err) 330 } 331 if len(kc.started) > 0 && !tc.ShouldBuild { 332 t.Errorf("Built but should not have: %+v", tc) 333 } else if len(kc.started) == 0 && tc.ShouldBuild { 334 t.Errorf("Not built but should have: %+v", tc) 335 } 336 if tc.ShouldComment && len(g.IssueCommentsAdded) == 0 { 337 t.Error("Expected comment to github") 338 } else if !tc.ShouldComment && len(g.IssueCommentsAdded) > 0 { 339 t.Errorf("Expected no comments to github, but got %d", len(g.CreatedStatuses)) 340 } 341 } 342 }