@@ -623,3 +623,103 @@ func TestPreflightChecks(t *testing.T) {
623
623
})
624
624
}
625
625
}
626
+
627
+ func TestPreflightChecksUpgradesDowngrades (t * testing.T ) {
628
+ testCases := []struct {
629
+ name string
630
+ installedVersion string
631
+ targetVersion string
632
+ expectedConditionStatus corev1.ConditionStatus
633
+ expectedError bool
634
+ }{
635
+ {
636
+ name : "upgrade core provider major version" ,
637
+ expectedConditionStatus : corev1 .ConditionTrue ,
638
+ installedVersion : "v1.9.0" ,
639
+ targetVersion : "v2.0.0" ,
640
+ },
641
+ {
642
+ name : "upgrade core provider minor version" ,
643
+ expectedConditionStatus : corev1 .ConditionTrue ,
644
+ installedVersion : "v1.9.0" ,
645
+ targetVersion : "v1.10.0" ,
646
+ },
647
+ {
648
+ name : "downgrade core provider major version" ,
649
+ expectedConditionStatus : corev1 .ConditionFalse ,
650
+ installedVersion : "v2.0.0" ,
651
+ targetVersion : "v1.9.0" ,
652
+ expectedError : true ,
653
+ },
654
+ {
655
+ name : "downgrade core provider minor version" ,
656
+ expectedConditionStatus : corev1 .ConditionFalse ,
657
+ installedVersion : "v1.10.0" ,
658
+ targetVersion : "v1.9.0" ,
659
+ expectedError : true ,
660
+ },
661
+ {
662
+ name : "downgrade core provider patch version" ,
663
+ expectedConditionStatus : corev1 .ConditionTrue ,
664
+ installedVersion : "v1.10.1" ,
665
+ targetVersion : "v1.10.0" ,
666
+ },
667
+ {
668
+ name : "same version" ,
669
+ expectedConditionStatus : corev1 .ConditionTrue ,
670
+ installedVersion : "v1.10.0" ,
671
+ targetVersion : "v1.10.0" ,
672
+ },
673
+ }
674
+
675
+ for _ , tc := range testCases {
676
+ t .Run (tc .name , func (t * testing.T ) {
677
+ gs := NewWithT (t )
678
+
679
+ provider := & operatorv1.CoreProvider {
680
+ ObjectMeta : metav1.ObjectMeta {
681
+ Name : "cluster-api" ,
682
+ Namespace : "provider-test-ns-1" ,
683
+ },
684
+ TypeMeta : metav1.TypeMeta {
685
+ Kind : "CoreProvider" ,
686
+ APIVersion : "operator.cluster.x-k8s.io/v1alpha1" ,
687
+ },
688
+ Spec : operatorv1.CoreProviderSpec {
689
+ ProviderSpec : operatorv1.ProviderSpec {
690
+ Version : tc .targetVersion ,
691
+ FetchConfig : & operatorv1.FetchConfiguration {
692
+ URL : "https://example.com" ,
693
+ },
694
+ },
695
+ },
696
+ Status : operatorv1.CoreProviderStatus {
697
+ ProviderStatus : operatorv1.ProviderStatus {
698
+ InstalledVersion : & tc .installedVersion ,
699
+ },
700
+ },
701
+ }
702
+
703
+ fakeclient := fake .NewClientBuilder ().WithObjects ().Build ()
704
+
705
+ gs .Expect (fakeclient .Create (ctx , provider )).To (Succeed ())
706
+
707
+ _ , err := preflightChecks (context .Background (), fakeclient , provider , & operatorv1.CoreProviderList {})
708
+ if tc .expectedError {
709
+ gs .Expect (err ).To (HaveOccurred ())
710
+ } else {
711
+ gs .Expect (err ).ToNot (HaveOccurred ())
712
+ }
713
+
714
+ // Check if proper condition is returned
715
+ gs .Expect (provider .GetStatus ().Conditions ).To (HaveLen (1 ))
716
+ gs .Expect (provider .GetStatus ().Conditions [0 ].Type ).To (Equal (operatorv1 .PreflightCheckCondition ))
717
+ gs .Expect (provider .GetStatus ().Conditions [0 ].Status ).To (Equal (tc .expectedConditionStatus ))
718
+
719
+ if tc .expectedConditionStatus == corev1 .ConditionFalse {
720
+ gs .Expect (provider .GetStatus ().Conditions [0 ].Reason ).To (Equal (operatorv1 .UnsupportedProviderDowngradeReason ))
721
+ gs .Expect (provider .GetStatus ().Conditions [0 ].Severity ).To (Equal (clusterv1 .ConditionSeverityError ))
722
+ }
723
+ })
724
+ }
725
+ }
0 commit comments