github.com/abayer/test-infra@v0.0.5/prow/plugins/size/size_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 size 18 19 import ( 20 "testing" 21 22 "github.com/sirupsen/logrus" 23 24 "k8s.io/test-infra/prow/github" 25 ) 26 27 type ghc struct { 28 *testing.T 29 labels map[github.Label]bool 30 files map[string][]byte 31 prChanges []github.PullRequestChange 32 33 addLabelErr, removeLabelErr, getIssueLabelsErr, 34 getFileErr, getPullRequestChangesErr error 35 } 36 37 func (c *ghc) AddLabel(_, _ string, _ int, label string) error { 38 c.T.Logf("AddLabel: %s", label) 39 c.labels[github.Label{Name: label}] = true 40 41 return c.addLabelErr 42 } 43 44 func (c *ghc) RemoveLabel(_, _ string, _ int, label string) error { 45 c.T.Logf("RemoveLabel: %s", label) 46 for k := range c.labels { 47 if k.Name == label { 48 delete(c.labels, k) 49 } 50 } 51 52 return c.removeLabelErr 53 } 54 55 func (c *ghc) GetIssueLabels(_, _ string, _ int) (ls []github.Label, err error) { 56 c.T.Log("GetIssueLabels") 57 for k, ok := range c.labels { 58 if ok { 59 ls = append(ls, k) 60 } 61 } 62 63 err = c.getIssueLabelsErr 64 return 65 } 66 67 func (c *ghc) GetFile(_, _, path, _ string) ([]byte, error) { 68 c.T.Logf("GetFile: %s", path) 69 return c.files[path], c.getFileErr 70 } 71 72 func (c *ghc) GetPullRequestChanges(_, _ string, _ int) ([]github.PullRequestChange, error) { 73 c.T.Log("GetPullRequestChanges") 74 return c.prChanges, c.getPullRequestChangesErr 75 } 76 77 func TestHandlePR(t *testing.T) { 78 cases := []struct { 79 name string 80 client *ghc 81 event github.PullRequestEvent 82 err error 83 finalLabels []github.Label 84 }{ 85 { 86 name: "simple size/S, no .generated_files", 87 client: &ghc{ 88 labels: map[github.Label]bool{}, 89 getFileErr: &github.FileNotFound{}, 90 prChanges: []github.PullRequestChange{ 91 { 92 SHA: "abcd", 93 Filename: "foobar", 94 Additions: 10, 95 Deletions: 10, 96 Changes: 20, 97 }, 98 { 99 SHA: "abcd", 100 Filename: "barfoo", 101 Additions: 3, 102 Deletions: 4, 103 Changes: 7, 104 }, 105 }, 106 }, 107 event: github.PullRequestEvent{ 108 Action: github.PullRequestActionOpened, 109 Number: 101, 110 PullRequest: github.PullRequest{ 111 Number: 101, 112 Base: github.PullRequestBranch{ 113 SHA: "abcd", 114 Repo: github.Repo{ 115 Owner: github.User{ 116 Login: "kubernetes", 117 }, 118 Name: "kubernetes", 119 }, 120 }, 121 }, 122 }, 123 finalLabels: []github.Label{ 124 {Name: "size/S"}, 125 }, 126 }, 127 { 128 name: "simple size/M, with .generated_files", 129 client: &ghc{ 130 labels: map[github.Label]bool{}, 131 files: map[string][]byte{ 132 ".generated_files": []byte(` 133 file-name foobar 134 135 path-prefix generated 136 `), 137 }, 138 prChanges: []github.PullRequestChange{ 139 { 140 SHA: "abcd", 141 Filename: "foobar", 142 Additions: 10, 143 Deletions: 10, 144 Changes: 20, 145 }, 146 { 147 SHA: "abcd", 148 Filename: "barfoo", 149 Additions: 50, 150 Deletions: 0, 151 Changes: 50, 152 }, 153 { 154 SHA: "abcd", 155 Filename: "generated/what.txt", 156 Additions: 30, 157 Deletions: 0, 158 Changes: 30, 159 }, 160 { 161 SHA: "abcd", 162 Filename: "generated/my/file.txt", 163 Additions: 300, 164 Deletions: 0, 165 Changes: 300, 166 }, 167 }, 168 }, 169 event: github.PullRequestEvent{ 170 Action: github.PullRequestActionOpened, 171 Number: 101, 172 PullRequest: github.PullRequest{ 173 Number: 101, 174 Base: github.PullRequestBranch{ 175 SHA: "abcd", 176 Repo: github.Repo{ 177 Owner: github.User{ 178 Login: "kubernetes", 179 }, 180 Name: "kubernetes", 181 }, 182 }, 183 }, 184 }, 185 finalLabels: []github.Label{ 186 {Name: "size/M"}, 187 }, 188 }, 189 { 190 name: "simple size/XS, with .generated_files and paths-from-repo", 191 client: &ghc{ 192 labels: map[github.Label]bool{}, 193 files: map[string][]byte{ 194 ".generated_files": []byte(` 195 # Comments 196 file-name foobar 197 198 path-prefix generated 199 200 paths-from-repo docs/.generated_docs 201 `), 202 "docs/.generated_docs": []byte(` 203 # Comments work 204 205 # And empty lines don't matter 206 foobar 207 mypath1 208 mypath2 209 mydir/mypath3 210 `), 211 }, 212 prChanges: []github.PullRequestChange{ 213 { 214 SHA: "abcd", 215 Filename: "foobar", 216 Additions: 10, 217 Deletions: 10, 218 Changes: 20, 219 }, 220 { // Notice "barfoo" is the only relevant change. 221 SHA: "abcd", 222 Filename: "barfoo", 223 Additions: 5, 224 Deletions: 0, 225 Changes: 5, 226 }, 227 { 228 SHA: "abcd", 229 Filename: "generated/what.txt", 230 Additions: 30, 231 Deletions: 0, 232 Changes: 30, 233 }, 234 { 235 SHA: "abcd", 236 Filename: "generated/my/file.txt", 237 Additions: 300, 238 Deletions: 0, 239 Changes: 300, 240 }, 241 { 242 SHA: "abcd", 243 Filename: "mypath1", 244 Additions: 300, 245 Deletions: 0, 246 Changes: 300, 247 }, 248 { 249 SHA: "abcd", 250 Filename: "mydir/mypath3", 251 Additions: 300, 252 Deletions: 0, 253 Changes: 300, 254 }, 255 }, 256 }, 257 event: github.PullRequestEvent{ 258 Action: github.PullRequestActionOpened, 259 Number: 101, 260 PullRequest: github.PullRequest{ 261 Number: 101, 262 Base: github.PullRequestBranch{ 263 SHA: "abcd", 264 Repo: github.Repo{ 265 Owner: github.User{ 266 Login: "kubernetes", 267 }, 268 Name: "kubernetes", 269 }, 270 }, 271 }, 272 }, 273 finalLabels: []github.Label{ 274 {Name: "size/XS"}, 275 }, 276 }, 277 { 278 name: "pr closed event", 279 client: &ghc{}, 280 event: github.PullRequestEvent{ 281 Action: github.PullRequestActionClosed, 282 }, 283 finalLabels: []github.Label{}, 284 }, 285 { 286 name: "XS -> S transition", 287 client: &ghc{ 288 labels: map[github.Label]bool{ 289 {Name: "irrelevant"}: true, 290 {Name: "size/XS"}: true, 291 }, 292 files: map[string][]byte{ 293 ".generated_files": []byte(` 294 # Comments 295 file-name foobar 296 297 path-prefix generated 298 299 paths-from-repo docs/.generated_docs 300 `), 301 "docs/.generated_docs": []byte(` 302 # Comments work 303 304 # And empty lines don't matter 305 foobar 306 mypath1 307 mypath2 308 mydir/mypath3 309 `), 310 }, 311 prChanges: []github.PullRequestChange{ 312 { 313 SHA: "abcd", 314 Filename: "foobar", 315 Additions: 10, 316 Deletions: 10, 317 Changes: 20, 318 }, 319 { // Notice "barfoo" is the only relevant change. 320 SHA: "abcd", 321 Filename: "barfoo", 322 Additions: 5, 323 Deletions: 0, 324 Changes: 5, 325 }, 326 { 327 SHA: "abcd", 328 Filename: "generated/what.txt", 329 Additions: 30, 330 Deletions: 0, 331 Changes: 30, 332 }, 333 { 334 SHA: "abcd", 335 Filename: "generated/my/file.txt", 336 Additions: 300, 337 Deletions: 0, 338 Changes: 300, 339 }, 340 { 341 SHA: "abcd", 342 Filename: "mypath1", 343 Additions: 300, 344 Deletions: 0, 345 Changes: 300, 346 }, 347 { 348 SHA: "abcd", 349 Filename: "mydir/mypath3", 350 Additions: 300, 351 Deletions: 0, 352 Changes: 300, 353 }, 354 }, 355 }, 356 event: github.PullRequestEvent{ 357 Action: github.PullRequestActionOpened, 358 Number: 101, 359 PullRequest: github.PullRequest{ 360 Number: 101, 361 Base: github.PullRequestBranch{ 362 SHA: "abcd", 363 Repo: github.Repo{ 364 Owner: github.User{ 365 Login: "kubernetes", 366 }, 367 Name: "kubernetes", 368 }, 369 }, 370 }, 371 }, 372 finalLabels: []github.Label{ 373 {Name: "irrelevant"}, 374 {Name: "size/XS"}, 375 }, 376 }, 377 { 378 name: "pull request reopened", 379 client: &ghc{ 380 labels: map[github.Label]bool{}, 381 getFileErr: &github.FileNotFound{}, 382 prChanges: []github.PullRequestChange{ 383 { 384 SHA: "abcd", 385 Filename: "foobar", 386 Additions: 10, 387 Deletions: 10, 388 Changes: 20, 389 }, 390 { 391 SHA: "abcd", 392 Filename: "barfoo", 393 Additions: 3, 394 Deletions: 4, 395 Changes: 7, 396 }, 397 }, 398 }, 399 event: github.PullRequestEvent{ 400 Action: github.PullRequestActionReopened, 401 Number: 101, 402 PullRequest: github.PullRequest{ 403 Number: 101, 404 Base: github.PullRequestBranch{ 405 SHA: "abcd", 406 Repo: github.Repo{ 407 Owner: github.User{ 408 Login: "kubernetes", 409 }, 410 Name: "kubernetes", 411 }, 412 }, 413 }, 414 }, 415 finalLabels: []github.Label{ 416 {Name: "size/S"}, 417 }, 418 }, 419 { 420 name: "pull request edited", 421 client: &ghc{ 422 labels: map[github.Label]bool{}, 423 getFileErr: &github.FileNotFound{}, 424 prChanges: []github.PullRequestChange{ 425 { 426 SHA: "abcd", 427 Filename: "foobar", 428 Additions: 30, 429 Deletions: 40, 430 Changes: 70, 431 }, 432 }, 433 }, 434 event: github.PullRequestEvent{ 435 Action: github.PullRequestActionEdited, 436 Number: 101, 437 PullRequest: github.PullRequest{ 438 Number: 101, 439 Base: github.PullRequestBranch{ 440 SHA: "abcd", 441 Repo: github.Repo{ 442 Owner: github.User{ 443 Login: "kubernetes", 444 }, 445 Name: "kubernetes", 446 }, 447 }, 448 }, 449 }, 450 finalLabels: []github.Label{ 451 {Name: "size/M"}, 452 }, 453 }, 454 } 455 456 for _, c := range cases { 457 t.Run(c.name, func(t *testing.T) { 458 if c.client == nil { 459 t.Fatalf("case can not have nil github client") 460 } 461 462 // Set up test logging. 463 c.client.T = t 464 465 err := handlePR(c.client, logrus.NewEntry(logrus.New()), c.event) 466 467 if err != nil && c.err == nil { 468 t.Fatalf("handlePR error: %v", err) 469 } 470 471 if err == nil && c.err != nil { 472 t.Fatalf("handlePR wanted error %v, got nil", err) 473 } 474 475 if got, want := err, c.err; got != nil && got.Error() != want.Error() { 476 t.Fatalf("handlePR errors mismatch: got %v, want %v", got, want) 477 } 478 479 if got, want := len(c.client.labels), len(c.finalLabels); got != want { 480 t.Logf("github client labels: got %v; want %v", c.client.labels, c.finalLabels) 481 t.Fatalf("finalLabels count mismatch: got %d, want %d", got, want) 482 } 483 484 for _, l := range c.finalLabels { 485 if !c.client.labels[l] { 486 t.Fatalf("github client labels missing %v", l) 487 } 488 } 489 }) 490 } 491 }