|
17 | 17 | defaultCguName = "cgu-test"
|
18 | 18 | defaultCguNsName = "test-ns"
|
19 | 19 | defaultCguMaxConcurrency = 1
|
| 20 | + defaultCguClusterName = "test-cluster" |
| 21 | + defaultCguClusterState = v1alpha1.NotStarted |
20 | 22 | defaultCguCondition = conditionComplete
|
21 | 23 | )
|
22 | 24 |
|
@@ -551,6 +553,168 @@ func TestCguWaitUntilComplete(t *testing.T) {
|
551 | 553 | }
|
552 | 554 | }
|
553 | 555 |
|
| 556 | +func TestCguWaitUntilClusterInState(t *testing.T) { |
| 557 | + testCases := []struct { |
| 558 | + cluster string |
| 559 | + state string |
| 560 | + exists bool |
| 561 | + inState bool |
| 562 | + valid bool |
| 563 | + expectedError error |
| 564 | + }{ |
| 565 | + { |
| 566 | + cluster: defaultCguClusterName, |
| 567 | + state: defaultCguClusterState, |
| 568 | + exists: true, |
| 569 | + inState: true, |
| 570 | + valid: true, |
| 571 | + expectedError: nil, |
| 572 | + }, |
| 573 | + { |
| 574 | + cluster: "", |
| 575 | + state: defaultCguClusterState, |
| 576 | + exists: true, |
| 577 | + inState: true, |
| 578 | + valid: true, |
| 579 | + expectedError: fmt.Errorf("cluster name cannot be empty"), |
| 580 | + }, |
| 581 | + { |
| 582 | + cluster: defaultCguClusterName, |
| 583 | + state: "", |
| 584 | + exists: true, |
| 585 | + inState: true, |
| 586 | + valid: true, |
| 587 | + expectedError: fmt.Errorf("state cannot be empty"), |
| 588 | + }, |
| 589 | + { |
| 590 | + cluster: defaultCguClusterName, |
| 591 | + state: defaultCguClusterState, |
| 592 | + exists: false, |
| 593 | + inState: true, |
| 594 | + valid: true, |
| 595 | + expectedError: fmt.Errorf("cgu object %s does not exist in namespace %s", defaultCguName, defaultCguNsName), |
| 596 | + }, |
| 597 | + { |
| 598 | + cluster: defaultCguClusterName, |
| 599 | + state: defaultCguClusterState, |
| 600 | + exists: true, |
| 601 | + inState: false, |
| 602 | + valid: true, |
| 603 | + expectedError: context.DeadlineExceeded, |
| 604 | + }, |
| 605 | + { |
| 606 | + cluster: defaultCguClusterName, |
| 607 | + state: defaultCguClusterState, |
| 608 | + exists: true, |
| 609 | + inState: true, |
| 610 | + valid: false, |
| 611 | + expectedError: fmt.Errorf("CGU 'nsname' cannot be empty"), |
| 612 | + }, |
| 613 | + } |
| 614 | + |
| 615 | + for _, testCase := range testCases { |
| 616 | + var ( |
| 617 | + runtimeObjects []runtime.Object |
| 618 | + cguBuilder *CguBuilder |
| 619 | + ) |
| 620 | + |
| 621 | + if testCase.exists { |
| 622 | + cgu := buildDummyCgu(defaultCguName, defaultCguNsName, defaultCguMaxConcurrency) |
| 623 | + |
| 624 | + if testCase.inState { |
| 625 | + cgu.Status.Status.CurrentBatchRemediationProgress = map[string]*v1alpha1.ClusterRemediationProgress{ |
| 626 | + testCase.cluster: {State: testCase.state}, |
| 627 | + } |
| 628 | + } |
| 629 | + |
| 630 | + runtimeObjects = append(runtimeObjects, cgu) |
| 631 | + } |
| 632 | + |
| 633 | + testSettings := clients.GetTestClients(clients.TestClientParams{ |
| 634 | + K8sMockObjects: runtimeObjects, |
| 635 | + }) |
| 636 | + |
| 637 | + if testCase.valid { |
| 638 | + cguBuilder = buildValidCguTestBuilder(testSettings) |
| 639 | + } else { |
| 640 | + cguBuilder = buildInvalidCguTestBuilder(testSettings) |
| 641 | + } |
| 642 | + |
| 643 | + _, err := cguBuilder.WaitUntilClusterInState(testCase.cluster, testCase.state, time.Second) |
| 644 | + assert.Equal(t, testCase.expectedError, err) |
| 645 | + } |
| 646 | +} |
| 647 | + |
| 648 | +func TestCguWaitUntilClusterComplete(t *testing.T) { |
| 649 | + testCases := []struct { |
| 650 | + complete bool |
| 651 | + expectedError error |
| 652 | + }{ |
| 653 | + { |
| 654 | + complete: true, |
| 655 | + expectedError: nil, |
| 656 | + }, |
| 657 | + { |
| 658 | + complete: false, |
| 659 | + expectedError: context.DeadlineExceeded, |
| 660 | + }, |
| 661 | + } |
| 662 | + |
| 663 | + for _, testCase := range testCases { |
| 664 | + cgu := buildDummyCgu(defaultCguName, defaultCguNsName, defaultCguMaxConcurrency) |
| 665 | + |
| 666 | + if testCase.complete { |
| 667 | + cgu.Status.Status.CurrentBatchRemediationProgress = map[string]*v1alpha1.ClusterRemediationProgress{ |
| 668 | + defaultCguClusterName: {State: v1alpha1.Completed}, |
| 669 | + } |
| 670 | + } |
| 671 | + |
| 672 | + testSettings := clients.GetTestClients(clients.TestClientParams{ |
| 673 | + K8sMockObjects: []runtime.Object{cgu}, |
| 674 | + }) |
| 675 | + |
| 676 | + cguBuilder := buildValidCguTestBuilder(testSettings) |
| 677 | + _, err := cguBuilder.WaitUntilClusterComplete(defaultCguClusterName, time.Second) |
| 678 | + |
| 679 | + assert.Equal(t, testCase.expectedError, err) |
| 680 | + } |
| 681 | +} |
| 682 | + |
| 683 | +func TestCguWaitUntilClusterInProgress(t *testing.T) { |
| 684 | + testCases := []struct { |
| 685 | + inProgress bool |
| 686 | + expectedError error |
| 687 | + }{ |
| 688 | + { |
| 689 | + inProgress: true, |
| 690 | + expectedError: nil, |
| 691 | + }, |
| 692 | + { |
| 693 | + inProgress: false, |
| 694 | + expectedError: context.DeadlineExceeded, |
| 695 | + }, |
| 696 | + } |
| 697 | + |
| 698 | + for _, testCase := range testCases { |
| 699 | + cgu := buildDummyCgu(defaultCguName, defaultCguNsName, defaultCguMaxConcurrency) |
| 700 | + |
| 701 | + if testCase.inProgress { |
| 702 | + cgu.Status.Status.CurrentBatchRemediationProgress = map[string]*v1alpha1.ClusterRemediationProgress{ |
| 703 | + defaultCguClusterName: {State: v1alpha1.InProgress}, |
| 704 | + } |
| 705 | + } |
| 706 | + |
| 707 | + testSettings := clients.GetTestClients(clients.TestClientParams{ |
| 708 | + K8sMockObjects: []runtime.Object{cgu}, |
| 709 | + }) |
| 710 | + |
| 711 | + cguBuilder := buildValidCguTestBuilder(testSettings) |
| 712 | + _, err := cguBuilder.WaitUntilClusterInProgress(defaultCguClusterName, time.Second) |
| 713 | + |
| 714 | + assert.Equal(t, testCase.expectedError, err) |
| 715 | + } |
| 716 | +} |
| 717 | + |
554 | 718 | func TestWaitUntilBackupStarts(t *testing.T) {
|
555 | 719 | cguObject := buildDummyCgu(defaultCguName, defaultCguNsName, defaultCguMaxConcurrency)
|
556 | 720 | cguObject.Status.Backup = &v1alpha1.BackupStatus{}
|
|
0 commit comments