github.com/giantswarm/apiextensions/v2@v2.6.2/pkg/apis/infrastructure/v1alpha2/common_cluster_status_funcs_test.go (about) 1 package v1alpha2 2 3 import ( 4 "reflect" 5 "strconv" 6 "testing" 7 "time" 8 9 "github.com/google/go-cmp/cmp" 10 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 11 ) 12 13 func Test_Provider_Status_LatestVersion(t *testing.T) { 14 testCases := []struct { 15 Name string 16 CommonClusterStatus CommonClusterStatus 17 ExpectedVersion string 18 }{ 19 { 20 Name: "case 0", 21 CommonClusterStatus: CommonClusterStatus{ 22 Versions: []CommonClusterStatusVersion{}, 23 }, 24 ExpectedVersion: "", 25 }, 26 { 27 Name: "case 1", 28 CommonClusterStatus: CommonClusterStatus{ 29 Versions: []CommonClusterStatusVersion{ 30 { 31 LastTransitionTime: metav1.Time{Time: time.Unix(10, 0)}, 32 Version: "1.0.0", 33 }, 34 }, 35 }, 36 ExpectedVersion: "1.0.0", 37 }, 38 { 39 Name: "case 2", 40 CommonClusterStatus: CommonClusterStatus{ 41 Versions: []CommonClusterStatusVersion{ 42 { 43 LastTransitionTime: metav1.Time{Time: time.Unix(10, 0)}, 44 Version: "1.0.0", 45 }, 46 { 47 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 48 Version: "2.0.0", 49 }, 50 }, 51 }, 52 ExpectedVersion: "2.0.0", 53 }, 54 { 55 Name: "case 3", 56 CommonClusterStatus: CommonClusterStatus{ 57 Versions: []CommonClusterStatusVersion{ 58 { 59 LastTransitionTime: metav1.Time{Time: time.Unix(10, 0)}, 60 Version: "1.0.0", 61 }, 62 { 63 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 64 Version: "2.0.0", 65 }, 66 { 67 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 68 Version: "3.0.0", 69 }, 70 }, 71 }, 72 ExpectedVersion: "3.0.0", 73 }, 74 { 75 Name: "case 4", 76 CommonClusterStatus: CommonClusterStatus{ 77 Versions: []CommonClusterStatusVersion{ 78 { 79 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 80 Version: "2.0.0", 81 }, 82 { 83 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 84 Version: "3.0.0", 85 }, 86 { 87 LastTransitionTime: metav1.Time{Time: time.Unix(10, 0)}, 88 Version: "1.0.0", 89 }, 90 }, 91 }, 92 ExpectedVersion: "3.0.0", 93 }, 94 { 95 Name: "case 5", 96 CommonClusterStatus: CommonClusterStatus{ 97 Versions: []CommonClusterStatusVersion{ 98 { 99 LastTransitionTime: metav1.Time{ 100 Time: time.Unix(20, 0), 101 }, 102 Version: "2.0.0", 103 }, 104 { 105 LastTransitionTime: metav1.Time{ 106 Time: time.Unix(30, 0), 107 }, 108 Version: "3.0.0", 109 }, 110 { 111 LastTransitionTime: metav1.Time{ 112 Time: time.Unix(10, 0), 113 }, 114 Version: "1.0.0", 115 }, 116 }, 117 }, 118 ExpectedVersion: "3.0.0", 119 }, 120 } 121 122 for _, tc := range testCases { 123 semver := tc.CommonClusterStatus.LatestVersion() 124 125 if semver != tc.ExpectedVersion { 126 t.Fatalf("expected %#v got %#v", tc.ExpectedVersion, semver) 127 } 128 } 129 } 130 131 func Test_Provider_Status_withCondition(t *testing.T) { 132 testCases := []struct { 133 name string 134 conditions []CommonClusterStatusCondition 135 condition CommonClusterStatusCondition 136 limit int 137 expectedConditions []CommonClusterStatusCondition 138 }{ 139 { 140 name: "case 0: the creation of the tenant cluster starts", 141 conditions: []CommonClusterStatusCondition{}, 142 condition: CommonClusterStatusCondition{ 143 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 144 Condition: ClusterStatusConditionCreating, 145 }, 146 limit: 2, 147 expectedConditions: []CommonClusterStatusCondition{ 148 { 149 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 150 Condition: ClusterStatusConditionCreating, 151 }, 152 }, 153 }, 154 { 155 name: "case 1: the creation of the tenant cluster ends", 156 conditions: []CommonClusterStatusCondition{ 157 { 158 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 159 Condition: ClusterStatusConditionCreating, 160 }, 161 }, 162 condition: CommonClusterStatusCondition{ 163 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 164 Condition: ClusterStatusConditionCreated, 165 }, 166 limit: 2, 167 expectedConditions: []CommonClusterStatusCondition{ 168 { 169 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 170 Condition: ClusterStatusConditionCreated, 171 }, 172 { 173 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 174 Condition: ClusterStatusConditionCreating, 175 }, 176 }, 177 }, 178 { 179 name: "case 2: the first update of the tenant cluster starts", 180 conditions: []CommonClusterStatusCondition{ 181 { 182 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 183 Condition: ClusterStatusConditionCreated, 184 }, 185 { 186 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 187 Condition: ClusterStatusConditionCreating, 188 }, 189 }, 190 condition: CommonClusterStatusCondition{ 191 LastTransitionTime: metav1.Time{Time: time.Unix(40, 0)}, 192 Condition: ClusterStatusConditionUpdating, 193 }, 194 limit: 2, 195 expectedConditions: []CommonClusterStatusCondition{ 196 { 197 LastTransitionTime: metav1.Time{Time: time.Unix(40, 0)}, 198 Condition: ClusterStatusConditionUpdating, 199 }, 200 { 201 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 202 Condition: ClusterStatusConditionCreated, 203 }, 204 { 205 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 206 Condition: ClusterStatusConditionCreating, 207 }, 208 }, 209 }, 210 { 211 name: "case 3: the first update of the tenant cluster ends", 212 conditions: []CommonClusterStatusCondition{ 213 { 214 LastTransitionTime: metav1.Time{Time: time.Unix(40, 0)}, 215 Condition: ClusterStatusConditionUpdating, 216 }, 217 { 218 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 219 Condition: ClusterStatusConditionCreated, 220 }, 221 { 222 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 223 Condition: ClusterStatusConditionCreating, 224 }, 225 }, 226 condition: CommonClusterStatusCondition{ 227 LastTransitionTime: metav1.Time{Time: time.Unix(50, 0)}, 228 Condition: ClusterStatusConditionUpdated, 229 }, 230 limit: 2, 231 expectedConditions: []CommonClusterStatusCondition{ 232 { 233 LastTransitionTime: metav1.Time{Time: time.Unix(50, 0)}, 234 Condition: ClusterStatusConditionUpdated, 235 }, 236 { 237 LastTransitionTime: metav1.Time{Time: time.Unix(40, 0)}, 238 Condition: ClusterStatusConditionUpdating, 239 }, 240 { 241 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 242 Condition: ClusterStatusConditionCreated, 243 }, 244 { 245 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 246 Condition: ClusterStatusConditionCreating, 247 }, 248 }, 249 }, 250 { 251 name: "case 4: the second update of the tenant cluster starts", 252 conditions: []CommonClusterStatusCondition{ 253 { 254 LastTransitionTime: metav1.Time{Time: time.Unix(50, 0)}, 255 Condition: ClusterStatusConditionUpdated, 256 }, 257 { 258 LastTransitionTime: metav1.Time{Time: time.Unix(40, 0)}, 259 Condition: ClusterStatusConditionUpdating, 260 }, 261 { 262 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 263 Condition: ClusterStatusConditionCreated, 264 }, 265 { 266 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 267 Condition: ClusterStatusConditionCreating, 268 }, 269 }, 270 condition: CommonClusterStatusCondition{ 271 LastTransitionTime: metav1.Time{Time: time.Unix(60, 0)}, 272 Condition: ClusterStatusConditionUpdating, 273 }, 274 limit: 2, 275 expectedConditions: []CommonClusterStatusCondition{ 276 { 277 LastTransitionTime: metav1.Time{Time: time.Unix(60, 0)}, 278 Condition: ClusterStatusConditionUpdating, 279 }, 280 { 281 LastTransitionTime: metav1.Time{Time: time.Unix(50, 0)}, 282 Condition: ClusterStatusConditionUpdated, 283 }, 284 { 285 LastTransitionTime: metav1.Time{Time: time.Unix(40, 0)}, 286 Condition: ClusterStatusConditionUpdating, 287 }, 288 { 289 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 290 Condition: ClusterStatusConditionCreated, 291 }, 292 { 293 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 294 Condition: ClusterStatusConditionCreating, 295 }, 296 }, 297 }, 298 { 299 name: "case 5: the second update of the tenant cluster ends", 300 conditions: []CommonClusterStatusCondition{ 301 { 302 LastTransitionTime: metav1.Time{Time: time.Unix(60, 0)}, 303 Condition: ClusterStatusConditionUpdating, 304 }, 305 { 306 LastTransitionTime: metav1.Time{Time: time.Unix(50, 0)}, 307 Condition: ClusterStatusConditionUpdated, 308 }, 309 { 310 LastTransitionTime: metav1.Time{Time: time.Unix(40, 0)}, 311 Condition: ClusterStatusConditionUpdating, 312 }, 313 { 314 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 315 Condition: ClusterStatusConditionCreated, 316 }, 317 { 318 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 319 Condition: ClusterStatusConditionCreating, 320 }, 321 }, 322 condition: CommonClusterStatusCondition{ 323 LastTransitionTime: metav1.Time{Time: time.Unix(70, 0)}, 324 Condition: ClusterStatusConditionUpdated, 325 }, 326 limit: 2, 327 expectedConditions: []CommonClusterStatusCondition{ 328 { 329 LastTransitionTime: metav1.Time{Time: time.Unix(70, 0)}, 330 Condition: ClusterStatusConditionUpdated, 331 }, 332 { 333 LastTransitionTime: metav1.Time{Time: time.Unix(60, 0)}, 334 Condition: ClusterStatusConditionUpdating, 335 }, 336 { 337 LastTransitionTime: metav1.Time{Time: time.Unix(50, 0)}, 338 Condition: ClusterStatusConditionUpdated, 339 }, 340 { 341 LastTransitionTime: metav1.Time{Time: time.Unix(40, 0)}, 342 Condition: ClusterStatusConditionUpdating, 343 }, 344 { 345 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 346 Condition: ClusterStatusConditionCreated, 347 }, 348 { 349 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 350 Condition: ClusterStatusConditionCreating, 351 }, 352 }, 353 }, 354 { 355 name: "case 6: the third update of the tenant cluster starts", 356 conditions: []CommonClusterStatusCondition{ 357 { 358 LastTransitionTime: metav1.Time{Time: time.Unix(70, 0)}, 359 Condition: ClusterStatusConditionUpdated, 360 }, 361 { 362 LastTransitionTime: metav1.Time{Time: time.Unix(60, 0)}, 363 Condition: ClusterStatusConditionUpdating, 364 }, 365 { 366 LastTransitionTime: metav1.Time{Time: time.Unix(50, 0)}, 367 Condition: ClusterStatusConditionUpdated, 368 }, 369 { 370 LastTransitionTime: metav1.Time{Time: time.Unix(40, 0)}, 371 Condition: ClusterStatusConditionUpdating, 372 }, 373 { 374 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 375 Condition: ClusterStatusConditionCreated, 376 }, 377 { 378 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 379 Condition: ClusterStatusConditionCreating, 380 }, 381 }, 382 condition: CommonClusterStatusCondition{ 383 LastTransitionTime: metav1.Time{Time: time.Unix(80, 0)}, 384 Condition: ClusterStatusConditionUpdating, 385 }, 386 limit: 2, 387 expectedConditions: []CommonClusterStatusCondition{ 388 { 389 LastTransitionTime: metav1.Time{Time: time.Unix(80, 0)}, 390 Condition: ClusterStatusConditionUpdating, 391 }, 392 { 393 LastTransitionTime: metav1.Time{Time: time.Unix(70, 0)}, 394 Condition: ClusterStatusConditionUpdated, 395 }, 396 { 397 LastTransitionTime: metav1.Time{Time: time.Unix(60, 0)}, 398 Condition: ClusterStatusConditionUpdating, 399 }, 400 { 401 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 402 Condition: ClusterStatusConditionCreated, 403 }, 404 { 405 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 406 Condition: ClusterStatusConditionCreating, 407 }, 408 }, 409 }, 410 { 411 name: "case 7: the third update of the tenant cluster ends", 412 conditions: []CommonClusterStatusCondition{ 413 { 414 LastTransitionTime: metav1.Time{Time: time.Unix(80, 0)}, 415 Condition: ClusterStatusConditionUpdating, 416 }, 417 { 418 LastTransitionTime: metav1.Time{Time: time.Unix(70, 0)}, 419 Condition: ClusterStatusConditionUpdated, 420 }, 421 { 422 LastTransitionTime: metav1.Time{Time: time.Unix(60, 0)}, 423 Condition: ClusterStatusConditionUpdating, 424 }, 425 { 426 LastTransitionTime: metav1.Time{Time: time.Unix(50, 0)}, 427 Condition: ClusterStatusConditionUpdated, 428 }, 429 { 430 LastTransitionTime: metav1.Time{Time: time.Unix(40, 0)}, 431 Condition: ClusterStatusConditionUpdating, 432 }, 433 { 434 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 435 Condition: ClusterStatusConditionCreated, 436 }, 437 { 438 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 439 Condition: ClusterStatusConditionCreating, 440 }, 441 }, 442 condition: CommonClusterStatusCondition{ 443 LastTransitionTime: metav1.Time{Time: time.Unix(90, 0)}, 444 Condition: ClusterStatusConditionUpdated, 445 }, 446 limit: 2, 447 expectedConditions: []CommonClusterStatusCondition{ 448 { 449 LastTransitionTime: metav1.Time{Time: time.Unix(90, 0)}, 450 Condition: ClusterStatusConditionUpdated, 451 }, 452 { 453 LastTransitionTime: metav1.Time{Time: time.Unix(80, 0)}, 454 Condition: ClusterStatusConditionUpdating, 455 }, 456 { 457 LastTransitionTime: metav1.Time{Time: time.Unix(70, 0)}, 458 Condition: ClusterStatusConditionUpdated, 459 }, 460 { 461 LastTransitionTime: metav1.Time{Time: time.Unix(60, 0)}, 462 Condition: ClusterStatusConditionUpdating, 463 }, 464 { 465 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 466 Condition: ClusterStatusConditionCreated, 467 }, 468 { 469 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 470 Condition: ClusterStatusConditionCreating, 471 }, 472 }, 473 }, 474 { 475 name: "case 8: the second update of the tenant cluster starts before the first ended", 476 conditions: []CommonClusterStatusCondition{ 477 { 478 LastTransitionTime: metav1.Time{Time: time.Unix(40, 0)}, 479 Condition: ClusterStatusConditionUpdating, 480 }, 481 { 482 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 483 Condition: ClusterStatusConditionCreated, 484 }, 485 { 486 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 487 Condition: ClusterStatusConditionCreating, 488 }, 489 }, 490 condition: CommonClusterStatusCondition{ 491 LastTransitionTime: metav1.Time{Time: time.Unix(50, 0)}, 492 Condition: ClusterStatusConditionUpdating, 493 }, 494 limit: 2, 495 expectedConditions: []CommonClusterStatusCondition{ 496 { 497 LastTransitionTime: metav1.Time{Time: time.Unix(50, 0)}, 498 Condition: ClusterStatusConditionUpdating, 499 }, 500 // This Updated condition is added automatically when adding the 501 // Updating condition twice. That way any failure tracking the 502 // conditions properly would be fixed on reconciliation to keep the 503 // integrity of the condition list. Only unfortunate effect is that the 504 // tracked timestamp for the automatically added condition is off and 505 // does not reflect the truth. 506 { 507 LastTransitionTime: metav1.Time{Time: time.Unix(50, -1)}, 508 Condition: ClusterStatusConditionUpdated, 509 }, 510 { 511 LastTransitionTime: metav1.Time{Time: time.Unix(40, 0)}, 512 Condition: ClusterStatusConditionUpdating, 513 }, 514 { 515 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 516 Condition: ClusterStatusConditionCreated, 517 }, 518 { 519 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 520 Condition: ClusterStatusConditionCreating, 521 }, 522 }, 523 }, 524 { 525 name: "case 9: the fourth update of the tenant cluster starts before the thrird ended", 526 conditions: []CommonClusterStatusCondition{ 527 { 528 LastTransitionTime: metav1.Time{Time: time.Unix(80, 0)}, 529 Condition: ClusterStatusConditionUpdating, 530 }, 531 { 532 LastTransitionTime: metav1.Time{Time: time.Unix(70, 0)}, 533 Condition: ClusterStatusConditionUpdated, 534 }, 535 { 536 LastTransitionTime: metav1.Time{Time: time.Unix(60, 0)}, 537 Condition: ClusterStatusConditionUpdating, 538 }, 539 { 540 LastTransitionTime: metav1.Time{Time: time.Unix(50, 0)}, 541 Condition: ClusterStatusConditionUpdated, 542 }, 543 { 544 LastTransitionTime: metav1.Time{Time: time.Unix(40, 0)}, 545 Condition: ClusterStatusConditionUpdating, 546 }, 547 { 548 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 549 Condition: ClusterStatusConditionCreated, 550 }, 551 { 552 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 553 Condition: ClusterStatusConditionCreating, 554 }, 555 }, 556 condition: CommonClusterStatusCondition{ 557 LastTransitionTime: metav1.Time{Time: time.Unix(90, 0)}, 558 Condition: ClusterStatusConditionUpdating, 559 }, 560 limit: 2, 561 expectedConditions: []CommonClusterStatusCondition{ 562 { 563 LastTransitionTime: metav1.Time{Time: time.Unix(90, 0)}, 564 Condition: ClusterStatusConditionUpdating, 565 }, 566 // This Updated condition is added automatically when adding the 567 // Updating condition twice. That way any failure tracking the 568 // conditions properly would be fixed on reconciliation to keep the 569 // integrity of the condition list. Only unfortunate effect is that the 570 // tracked timestamp for the automatically added condition is off and 571 // does not reflect the truth. 572 { 573 LastTransitionTime: metav1.Time{Time: time.Unix(90, -1)}, 574 Condition: ClusterStatusConditionUpdated, 575 }, 576 { 577 LastTransitionTime: metav1.Time{Time: time.Unix(80, 0)}, 578 Condition: ClusterStatusConditionUpdating, 579 }, 580 { 581 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 582 Condition: ClusterStatusConditionCreated, 583 }, 584 { 585 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 586 Condition: ClusterStatusConditionCreating, 587 }, 588 }, 589 }, 590 } 591 592 for i, tc := range testCases { 593 t.Run(strconv.Itoa(i), func(t *testing.T) { 594 conditions := withCondition(tc.conditions, tc.condition, tc.limit) 595 596 if !reflect.DeepEqual(conditions, tc.expectedConditions) { 597 t.Fatalf("\n\n%s\n", cmp.Diff(conditions, tc.expectedConditions)) 598 } 599 }) 600 } 601 } 602 603 func Test_Provider_Status_withVersion(t *testing.T) { 604 testCases := []struct { 605 Name string 606 Versions []CommonClusterStatusVersion 607 Version CommonClusterStatusVersion 608 Limit int 609 ExpectedVersions []CommonClusterStatusVersion 610 }{ 611 { 612 Name: "case 0: list with zero items results in a list with one item", 613 Versions: []CommonClusterStatusVersion{}, 614 Version: CommonClusterStatusVersion{ 615 LastTransitionTime: metav1.Time{Time: time.Unix(10, 0)}, 616 Version: "1.0.0", 617 }, 618 Limit: 3, 619 ExpectedVersions: []CommonClusterStatusVersion{ 620 { 621 LastTransitionTime: metav1.Time{Time: time.Unix(10, 0)}, 622 Version: "1.0.0", 623 }, 624 }, 625 }, 626 { 627 Name: "case 1: list with one item results in a list with two items", 628 Versions: []CommonClusterStatusVersion{ 629 { 630 LastTransitionTime: metav1.Time{Time: time.Unix(10, 0)}, 631 Version: "1.0.0", 632 }, 633 }, 634 Version: CommonClusterStatusVersion{ 635 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 636 Version: "1.1.0", 637 }, 638 Limit: 3, 639 ExpectedVersions: []CommonClusterStatusVersion{ 640 { 641 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 642 Version: "1.1.0", 643 }, 644 { 645 LastTransitionTime: metav1.Time{Time: time.Unix(10, 0)}, 646 Version: "1.0.0", 647 }, 648 }, 649 }, 650 { 651 Name: "case 2: list with two items results in a list with three items", 652 Versions: []CommonClusterStatusVersion{ 653 { 654 LastTransitionTime: metav1.Time{Time: time.Unix(10, 0)}, 655 Version: "1.0.0", 656 }, 657 { 658 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 659 Version: "1.1.0", 660 }, 661 }, 662 Version: CommonClusterStatusVersion{ 663 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 664 Version: "1.5.0", 665 }, 666 Limit: 3, 667 ExpectedVersions: []CommonClusterStatusVersion{ 668 { 669 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 670 Version: "1.5.0", 671 }, 672 { 673 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 674 Version: "1.1.0", 675 }, 676 { 677 LastTransitionTime: metav1.Time{Time: time.Unix(10, 0)}, 678 Version: "1.0.0", 679 }, 680 }, 681 }, 682 { 683 Name: "case 3: list with three items results in a list with three items due to limit", 684 Versions: []CommonClusterStatusVersion{ 685 { 686 LastTransitionTime: metav1.Time{Time: time.Unix(10, 0)}, 687 Version: "1.0.0", 688 }, 689 { 690 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 691 Version: "1.1.0", 692 }, 693 { 694 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 695 Version: "1.5.0", 696 }, 697 }, 698 Version: CommonClusterStatusVersion{ 699 LastTransitionTime: metav1.Time{Time: time.Unix(40, 0)}, 700 Version: "3.0.0", 701 }, 702 Limit: 3, 703 ExpectedVersions: []CommonClusterStatusVersion{ 704 { 705 LastTransitionTime: metav1.Time{Time: time.Unix(40, 0)}, 706 Version: "3.0.0", 707 }, 708 { 709 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 710 Version: "1.5.0", 711 }, 712 { 713 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 714 Version: "1.1.0", 715 }, 716 }, 717 }, 718 { 719 Name: "case 4: list with five items results in a list with three items due to limit", 720 Versions: []CommonClusterStatusVersion{ 721 { 722 LastTransitionTime: metav1.Time{Time: time.Unix(10, 0)}, 723 Version: "1.0.0", 724 }, 725 { 726 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 727 Version: "1.1.0", 728 }, 729 { 730 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 731 Version: "1.5.0", 732 }, 733 { 734 LastTransitionTime: metav1.Time{Time: time.Unix(40, 0)}, 735 Version: "3.0.0", 736 }, 737 { 738 LastTransitionTime: metav1.Time{Time: time.Unix(50, 0)}, 739 Version: "3.2.0", 740 }, 741 }, 742 Version: CommonClusterStatusVersion{ 743 LastTransitionTime: metav1.Time{Time: time.Unix(60, 0)}, 744 Version: "3.3.0", 745 }, 746 Limit: 3, 747 ExpectedVersions: []CommonClusterStatusVersion{ 748 { 749 LastTransitionTime: metav1.Time{Time: time.Unix(60, 0)}, 750 Version: "3.3.0", 751 }, 752 { 753 LastTransitionTime: metav1.Time{Time: time.Unix(50, 0)}, 754 Version: "3.2.0", 755 }, 756 { 757 LastTransitionTime: metav1.Time{Time: time.Unix(40, 0)}, 758 Version: "3.0.0", 759 }, 760 }, 761 }, 762 { 763 Name: "case 5: same as 4 but checking items are ordered by date before cutting off", 764 Versions: []CommonClusterStatusVersion{ 765 { 766 LastTransitionTime: metav1.Time{Time: time.Unix(40, 0)}, 767 Version: "3.0.0", 768 }, 769 { 770 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 771 Version: "1.1.0", 772 }, 773 { 774 LastTransitionTime: metav1.Time{Time: time.Unix(10, 0)}, 775 Version: "1.0.0", 776 }, 777 { 778 LastTransitionTime: metav1.Time{Time: time.Unix(50, 0)}, 779 Version: "3.2.0", 780 }, 781 { 782 LastTransitionTime: metav1.Time{Time: time.Unix(30, 0)}, 783 Version: "1.5.0", 784 }, 785 }, 786 Version: CommonClusterStatusVersion{ 787 LastTransitionTime: metav1.Time{Time: time.Unix(60, 0)}, 788 Version: "3.3.0", 789 }, 790 Limit: 3, 791 ExpectedVersions: []CommonClusterStatusVersion{ 792 { 793 LastTransitionTime: metav1.Time{Time: time.Unix(60, 0)}, 794 Version: "3.3.0", 795 }, 796 { 797 LastTransitionTime: metav1.Time{Time: time.Unix(50, 0)}, 798 Version: "3.2.0", 799 }, 800 { 801 LastTransitionTime: metav1.Time{Time: time.Unix(40, 0)}, 802 Version: "3.0.0", 803 }, 804 }, 805 }, 806 { 807 Name: "case 6: list with one item results in a list with one item in case the version already exists", 808 Versions: []CommonClusterStatusVersion{ 809 { 810 LastTransitionTime: metav1.Time{Time: time.Unix(10, 0)}, 811 Version: "1.0.0", 812 }, 813 }, 814 Version: CommonClusterStatusVersion{ 815 LastTransitionTime: metav1.Time{Time: time.Unix(20, 0)}, 816 Version: "1.0.0", 817 }, 818 Limit: 3, 819 ExpectedVersions: []CommonClusterStatusVersion{ 820 { 821 LastTransitionTime: metav1.Time{Time: time.Unix(10, 0)}, 822 Version: "1.0.0", 823 }, 824 }, 825 }, 826 } 827 828 for i, tc := range testCases { 829 t.Run(strconv.Itoa(i), func(t *testing.T) { 830 versions := withVersion(tc.Versions, tc.Version, tc.Limit) 831 832 if !reflect.DeepEqual(versions, tc.ExpectedVersions) { 833 t.Fatalf("\n\n%s\n", cmp.Diff(versions, tc.ExpectedVersions)) 834 } 835 }) 836 } 837 }