@@ -17,13 +17,17 @@ limitations under the License.
17
17
package persistentvolume
18
18
19
19
import (
20
+ "fmt"
20
21
"testing"
21
22
22
23
v1 "k8s.io/api/core/v1"
23
24
storage "k8s.io/api/storage/v1"
24
25
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
+ utilfeature "k8s.io/apiserver/pkg/util/feature"
27
+ featuregatetesting "k8s.io/component-base/featuregate/testing"
25
28
"k8s.io/component-helpers/storage/volume"
26
29
"k8s.io/klog/v2/ktesting"
30
+ "k8s.io/kubernetes/pkg/features"
27
31
)
28
32
29
33
// Test single call to syncClaim and syncVolume methods.
@@ -750,13 +754,73 @@ func TestSync(t *testing.T) {
750
754
test : testSyncClaim ,
751
755
},
752
756
}
753
- _ , ctx := ktesting .NewTestContext (t )
754
- runSyncTests (t , ctx , tests , []* storage.StorageClass {
757
+
758
+ // Once the feature-gate VolumeAttributesClass is promoted to GA, merge it with the above tests
759
+ whenFeatureGateEnabled := []controllerTest {
760
+ {
761
+ // syncClaim with claim pre-bound to a PV that exists and is
762
+ // unbound, but its volume attributes class does not match. Check that the claim status is reset to Pending
763
+ name : "2-11 - claim prebound to unbound volume that volume attributes class is different" ,
764
+ initialVolumes : volumesWithVAC (classGold , newVolumeArray ("volume2-11" , "1Gi" , "" , "" , v1 .VolumeAvailable , v1 .PersistentVolumeReclaimRetain , classEmpty )),
765
+ expectedVolumes : volumesWithVAC (classGold , newVolumeArray ("volume2-11" , "1Gi" , "" , "" , v1 .VolumeAvailable , v1 .PersistentVolumeReclaimRetain , classEmpty )),
766
+ initialClaims : newClaimArray ("claim2-11" , "uid2-11" , "1Gi" , "volume2-11" , v1 .ClaimBound , nil ),
767
+ expectedClaims : newClaimArray ("claim2-11" , "uid2-11" , "1Gi" , "volume2-11" , v1 .ClaimPending , nil ),
768
+ expectedEvents : []string {"Warning VolumeMismatch" },
769
+ errors : noerrors ,
770
+ test : testSyncClaim ,
771
+ },
755
772
{
756
- ObjectMeta : metav1.ObjectMeta {Name : classWait },
757
- VolumeBindingMode : & modeWait ,
773
+ // syncClaim with claim pre-bound to a PV that exists and is
774
+ // unbound, they have the same volume attributes class. Check it gets bound and no volume.AnnBoundByController is set.
775
+ name : "2-12 - claim prebound to unbound volume that volume attributes class is same" ,
776
+ initialVolumes : volumesWithVAC (classGold , newVolumeArray ("volume2-12" , "1Gi" , "" , "" , v1 .VolumeAvailable , v1 .PersistentVolumeReclaimRetain , classEmpty )),
777
+ expectedVolumes : volumesWithVAC (classGold , newVolumeArray ("volume2-12" , "1Gi" , "uid2-12" , "claim2-12" , v1 .VolumeBound , v1 .PersistentVolumeReclaimRetain , classEmpty , volume .AnnBoundByController )),
778
+ initialClaims : claimWithVAC (& classGold , newClaimArray ("claim2-12" , "uid2-12" , "1Gi" , "volume2-12" , v1 .ClaimPending , nil )),
779
+ expectedClaims : withExpectedVAC (& classGold , claimWithVAC (& classGold , newClaimArray ("claim2-12" , "uid2-12" , "1Gi" , "volume2-12" , v1 .ClaimBound , nil , volume .AnnBindCompleted ))),
780
+ expectedEvents : noevents ,
781
+ errors : noerrors ,
782
+ test : testSyncClaim ,
758
783
},
759
- }, []* v1.Pod {})
784
+ }
785
+
786
+ // Once the feature-gate VolumeAttributesClass is promoted to GA, remove it
787
+ whenFeatureGateDisabled := []controllerTest {
788
+ {
789
+ // syncClaim with claim pre-bound to a PV that exists and is
790
+ // unbound, they have the same volume attributes class but the feature-gate is disabled. Check that the claim status is reset to Pending
791
+ name : "2-13 - claim prebound to unbound volume that volume attributes class is same" ,
792
+ initialVolumes : volumesWithVAC (classGold , newVolumeArray ("volume2-13" , "1Gi" , "" , "" , v1 .VolumeAvailable , v1 .PersistentVolumeReclaimRetain , classEmpty )),
793
+ expectedVolumes : volumesWithVAC (classGold , newVolumeArray ("volume2-13" , "1Gi" , "" , "" , v1 .VolumeAvailable , v1 .PersistentVolumeReclaimRetain , classEmpty )),
794
+ initialClaims : withExpectedVAC (& classGold , claimWithVAC (& classGold , newClaimArray ("claim2-13" , "uid2-13" , "1Gi" , "volume2-13" , v1 .ClaimBound , nil ))),
795
+ expectedClaims : claimWithVAC (& classGold , newClaimArray ("claim2-13" , "uid2-13" , "1Gi" , "volume2-13" , v1 .ClaimPending , nil )),
796
+ expectedEvents : []string {"Warning VolumeMismatch" },
797
+ errors : noerrors ,
798
+ test : testSyncClaim ,
799
+ },
800
+ }
801
+
802
+ for _ , isEnabled := range []bool {true , false } {
803
+ featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .VolumeAttributesClass , isEnabled )
804
+
805
+ allTests := tests
806
+ if isEnabled {
807
+ allTests = append (allTests , whenFeatureGateEnabled ... )
808
+ } else {
809
+ allTests = append (allTests , whenFeatureGateDisabled ... )
810
+ }
811
+
812
+ for i := range allTests {
813
+ allTests [i ].name = fmt .Sprintf ("features.VolumeAttributesClass=%v %s" , isEnabled , allTests [i ].name )
814
+ }
815
+
816
+ _ , ctx := ktesting .NewTestContext (t )
817
+ runSyncTests (t , ctx , allTests , []* storage.StorageClass {
818
+ {
819
+ ObjectMeta : metav1.ObjectMeta {Name : classWait },
820
+ VolumeBindingMode : & modeWait ,
821
+ },
822
+ }, []* v1.Pod {})
823
+ }
760
824
}
761
825
762
826
func TestSyncBlockVolume (t * testing.T ) {
0 commit comments