CSIStorageCapacity: prepare tests for enabling the feature by default

Defaults and validation are such that the field has to be set when
the feature is enabled, just as for the other boolean fields. This
was missing in some tests, which was okay as long as they ran
with the feature disabled. Once it gets enabled, validation will
flag the missing field as error.

Other tests didn't run at all.
This commit is contained in:
Patrick Ohly 2021-03-03 21:01:05 +01:00
parent 72d92159bc
commit 504f105a9e
3 changed files with 116 additions and 14 deletions

View File

@ -1666,6 +1666,8 @@ func TestCSIDriverValidation(t *testing.T) {
podInfoOnMount := true podInfoOnMount := true
notPodInfoOnMount := false notPodInfoOnMount := false
notRequiresRepublish := false notRequiresRepublish := false
storageCapacity := true
notStorageCapacity := false
supportedFSGroupPolicy := storage.FileFSGroupPolicy supportedFSGroupPolicy := storage.FileFSGroupPolicy
invalidFSGroupPolicy := storage.ReadWriteOnceWithFSTypeFSGroupPolicy invalidFSGroupPolicy := storage.ReadWriteOnceWithFSTypeFSGroupPolicy
invalidFSGroupPolicy = "invalid-mode" invalidFSGroupPolicy = "invalid-mode"
@ -1676,6 +1678,7 @@ func TestCSIDriverValidation(t *testing.T) {
AttachRequired: &attachRequired, AttachRequired: &attachRequired,
PodInfoOnMount: &podInfoOnMount, PodInfoOnMount: &podInfoOnMount,
RequiresRepublish: &notRequiresRepublish, RequiresRepublish: &notRequiresRepublish,
StorageCapacity: &storageCapacity,
}, },
}, },
{ {
@ -1685,6 +1688,7 @@ func TestCSIDriverValidation(t *testing.T) {
AttachRequired: &attachRequired, AttachRequired: &attachRequired,
PodInfoOnMount: &podInfoOnMount, PodInfoOnMount: &podInfoOnMount,
RequiresRepublish: &notRequiresRepublish, RequiresRepublish: &notRequiresRepublish,
StorageCapacity: &notStorageCapacity,
}, },
}, },
{ {
@ -1694,6 +1698,7 @@ func TestCSIDriverValidation(t *testing.T) {
AttachRequired: &attachRequired, AttachRequired: &attachRequired,
PodInfoOnMount: &notPodInfoOnMount, PodInfoOnMount: &notPodInfoOnMount,
RequiresRepublish: &notRequiresRepublish, RequiresRepublish: &notRequiresRepublish,
StorageCapacity: &storageCapacity,
}, },
}, },
{ {
@ -1703,6 +1708,7 @@ func TestCSIDriverValidation(t *testing.T) {
AttachRequired: &attachRequired, AttachRequired: &attachRequired,
PodInfoOnMount: &podInfoOnMount, PodInfoOnMount: &podInfoOnMount,
RequiresRepublish: &notRequiresRepublish, RequiresRepublish: &notRequiresRepublish,
StorageCapacity: &storageCapacity,
}, },
}, },
{ {
@ -1712,6 +1718,7 @@ func TestCSIDriverValidation(t *testing.T) {
AttachRequired: &attachRequired, AttachRequired: &attachRequired,
PodInfoOnMount: &podInfoOnMount, PodInfoOnMount: &podInfoOnMount,
RequiresRepublish: &notRequiresRepublish, RequiresRepublish: &notRequiresRepublish,
StorageCapacity: &storageCapacity,
}, },
}, },
{ {
@ -1720,6 +1727,7 @@ func TestCSIDriverValidation(t *testing.T) {
AttachRequired: &attachRequired, AttachRequired: &attachRequired,
PodInfoOnMount: &notPodInfoOnMount, PodInfoOnMount: &notPodInfoOnMount,
RequiresRepublish: &notRequiresRepublish, RequiresRepublish: &notRequiresRepublish,
StorageCapacity: &storageCapacity,
}, },
}, },
{ {
@ -1728,6 +1736,7 @@ func TestCSIDriverValidation(t *testing.T) {
AttachRequired: &attachRequired, AttachRequired: &attachRequired,
PodInfoOnMount: &podInfoOnMount, PodInfoOnMount: &podInfoOnMount,
RequiresRepublish: &notRequiresRepublish, RequiresRepublish: &notRequiresRepublish,
StorageCapacity: &storageCapacity,
}, },
}, },
{ {
@ -1736,6 +1745,7 @@ func TestCSIDriverValidation(t *testing.T) {
AttachRequired: &attachNotRequired, AttachRequired: &attachNotRequired,
PodInfoOnMount: &notPodInfoOnMount, PodInfoOnMount: &notPodInfoOnMount,
RequiresRepublish: &notRequiresRepublish, RequiresRepublish: &notRequiresRepublish,
StorageCapacity: &storageCapacity,
}, },
}, },
{ {
@ -1744,6 +1754,7 @@ func TestCSIDriverValidation(t *testing.T) {
AttachRequired: &attachNotRequired, AttachRequired: &attachNotRequired,
PodInfoOnMount: &notPodInfoOnMount, PodInfoOnMount: &notPodInfoOnMount,
RequiresRepublish: &notRequiresRepublish, RequiresRepublish: &notRequiresRepublish,
StorageCapacity: &storageCapacity,
VolumeLifecycleModes: []storage.VolumeLifecycleMode{ VolumeLifecycleModes: []storage.VolumeLifecycleMode{
storage.VolumeLifecyclePersistent, storage.VolumeLifecyclePersistent,
}, },
@ -1755,6 +1766,7 @@ func TestCSIDriverValidation(t *testing.T) {
AttachRequired: &attachNotRequired, AttachRequired: &attachNotRequired,
PodInfoOnMount: &notPodInfoOnMount, PodInfoOnMount: &notPodInfoOnMount,
RequiresRepublish: &notRequiresRepublish, RequiresRepublish: &notRequiresRepublish,
StorageCapacity: &storageCapacity,
VolumeLifecycleModes: []storage.VolumeLifecycleMode{ VolumeLifecycleModes: []storage.VolumeLifecycleMode{
storage.VolumeLifecycleEphemeral, storage.VolumeLifecycleEphemeral,
}, },
@ -1766,6 +1778,7 @@ func TestCSIDriverValidation(t *testing.T) {
AttachRequired: &attachNotRequired, AttachRequired: &attachNotRequired,
PodInfoOnMount: &notPodInfoOnMount, PodInfoOnMount: &notPodInfoOnMount,
RequiresRepublish: &notRequiresRepublish, RequiresRepublish: &notRequiresRepublish,
StorageCapacity: &storageCapacity,
VolumeLifecycleModes: []storage.VolumeLifecycleMode{ VolumeLifecycleModes: []storage.VolumeLifecycleMode{
storage.VolumeLifecycleEphemeral, storage.VolumeLifecycleEphemeral,
storage.VolumeLifecyclePersistent, storage.VolumeLifecyclePersistent,
@ -1778,6 +1791,7 @@ func TestCSIDriverValidation(t *testing.T) {
AttachRequired: &attachNotRequired, AttachRequired: &attachNotRequired,
PodInfoOnMount: &notPodInfoOnMount, PodInfoOnMount: &notPodInfoOnMount,
RequiresRepublish: &notRequiresRepublish, RequiresRepublish: &notRequiresRepublish,
StorageCapacity: &storageCapacity,
VolumeLifecycleModes: []storage.VolumeLifecycleMode{ VolumeLifecycleModes: []storage.VolumeLifecycleMode{
storage.VolumeLifecycleEphemeral, storage.VolumeLifecycleEphemeral,
storage.VolumeLifecyclePersistent, storage.VolumeLifecyclePersistent,
@ -1791,6 +1805,7 @@ func TestCSIDriverValidation(t *testing.T) {
AttachRequired: &attachNotRequired, AttachRequired: &attachNotRequired,
PodInfoOnMount: &notPodInfoOnMount, PodInfoOnMount: &notPodInfoOnMount,
RequiresRepublish: &notRequiresRepublish, RequiresRepublish: &notRequiresRepublish,
StorageCapacity: &storageCapacity,
FSGroupPolicy: &supportedFSGroupPolicy, FSGroupPolicy: &supportedFSGroupPolicy,
}, },
}, },
@ -1805,39 +1820,53 @@ func TestCSIDriverValidation(t *testing.T) {
{ {
ObjectMeta: metav1.ObjectMeta{Name: invalidName}, ObjectMeta: metav1.ObjectMeta{Name: invalidName},
Spec: storage.CSIDriverSpec{ Spec: storage.CSIDriverSpec{
AttachRequired: &attachRequired, AttachRequired: &attachRequired,
PodInfoOnMount: &podInfoOnMount, PodInfoOnMount: &podInfoOnMount,
StorageCapacity: &storageCapacity,
}, },
}, },
{ {
ObjectMeta: metav1.ObjectMeta{Name: longName}, ObjectMeta: metav1.ObjectMeta{Name: longName},
Spec: storage.CSIDriverSpec{ Spec: storage.CSIDriverSpec{
AttachRequired: &attachNotRequired, AttachRequired: &attachNotRequired,
PodInfoOnMount: &notPodInfoOnMount, PodInfoOnMount: &notPodInfoOnMount,
StorageCapacity: &storageCapacity,
}, },
}, },
{ {
// AttachRequired not set // AttachRequired not set
ObjectMeta: metav1.ObjectMeta{Name: driverName}, ObjectMeta: metav1.ObjectMeta{Name: driverName},
Spec: storage.CSIDriverSpec{ Spec: storage.CSIDriverSpec{
AttachRequired: nil, AttachRequired: nil,
PodInfoOnMount: &podInfoOnMount, PodInfoOnMount: &podInfoOnMount,
StorageCapacity: &storageCapacity,
}, },
}, },
{ {
// PodInfoOnMount not set // PodInfoOnMount not set
ObjectMeta: metav1.ObjectMeta{Name: driverName}, ObjectMeta: metav1.ObjectMeta{Name: driverName},
Spec: storage.CSIDriverSpec{ Spec: storage.CSIDriverSpec{
AttachRequired: &attachNotRequired, AttachRequired: &attachNotRequired,
PodInfoOnMount: nil, PodInfoOnMount: nil,
StorageCapacity: &storageCapacity,
},
},
{
// StorageCapacity not set
ObjectMeta: metav1.ObjectMeta{Name: driverName},
Spec: storage.CSIDriverSpec{
AttachRequired: &attachNotRequired,
PodInfoOnMount: &podInfoOnMount,
StorageCapacity: nil,
}, },
}, },
{ {
// invalid mode // invalid mode
ObjectMeta: metav1.ObjectMeta{Name: driverName}, ObjectMeta: metav1.ObjectMeta{Name: driverName},
Spec: storage.CSIDriverSpec{ Spec: storage.CSIDriverSpec{
AttachRequired: &attachNotRequired, AttachRequired: &attachNotRequired,
PodInfoOnMount: &notPodInfoOnMount, PodInfoOnMount: &notPodInfoOnMount,
StorageCapacity: &storageCapacity,
VolumeLifecycleModes: []storage.VolumeLifecycleMode{ VolumeLifecycleModes: []storage.VolumeLifecycleMode{
"no-such-mode", "no-such-mode",
}, },
@ -1847,9 +1876,10 @@ func TestCSIDriverValidation(t *testing.T) {
// invalid fsGroupPolicy // invalid fsGroupPolicy
ObjectMeta: metav1.ObjectMeta{Name: driverName}, ObjectMeta: metav1.ObjectMeta{Name: driverName},
Spec: storage.CSIDriverSpec{ Spec: storage.CSIDriverSpec{
AttachRequired: &attachNotRequired, AttachRequired: &attachNotRequired,
PodInfoOnMount: &notPodInfoOnMount, PodInfoOnMount: &notPodInfoOnMount,
FSGroupPolicy: &invalidFSGroupPolicy, FSGroupPolicy: &invalidFSGroupPolicy,
StorageCapacity: &storageCapacity,
}, },
}, },
} }
@ -1868,8 +1898,10 @@ func TestCSIDriverValidationUpdate(t *testing.T) {
attachRequired := true attachRequired := true
attachNotRequired := false attachNotRequired := false
podInfoOnMount := true podInfoOnMount := true
storageCapacity := true
notPodInfoOnMount := false notPodInfoOnMount := false
notRequiresRepublish := false notRequiresRepublish := false
notStorageCapacity := false
resourceVersion := "1" resourceVersion := "1"
invalidFSGroupPolicy := storage.ReadWriteOnceWithFSTypeFSGroupPolicy invalidFSGroupPolicy := storage.ReadWriteOnceWithFSTypeFSGroupPolicy
invalidFSGroupPolicy = "invalid-mode" invalidFSGroupPolicy = "invalid-mode"
@ -1883,6 +1915,7 @@ func TestCSIDriverValidationUpdate(t *testing.T) {
storage.VolumeLifecycleEphemeral, storage.VolumeLifecycleEphemeral,
storage.VolumeLifecyclePersistent, storage.VolumeLifecyclePersistent,
}, },
StorageCapacity: &storageCapacity,
}, },
} }
@ -1900,7 +1933,8 @@ func TestCSIDriverValidationUpdate(t *testing.T) {
storage.VolumeLifecycleEphemeral, storage.VolumeLifecycleEphemeral,
storage.VolumeLifecyclePersistent, storage.VolumeLifecyclePersistent,
}, },
FSGroupPolicy: &invalidFSGroupPolicy, FSGroupPolicy: &invalidFSGroupPolicy,
StorageCapacity: &storageCapacity,
}, },
}, },
} }
@ -1997,6 +2031,12 @@ func TestCSIDriverValidationUpdate(t *testing.T) {
new.Spec.FSGroupPolicy = &fileFSGroupPolicy new.Spec.FSGroupPolicy = &fileFSGroupPolicy
}, },
}, },
{
name: "StorageCapacity changed",
modify: func(new *storage.CSIDriver) {
new.Spec.StorageCapacity = &notStorageCapacity
},
},
} }
for _, test := range errorCases { for _, test := range errorCases {
@ -2010,6 +2050,48 @@ func TestCSIDriverValidationUpdate(t *testing.T) {
} }
} }
func TestCSIDriverStorageCapacityEnablement(t *testing.T) {
run := func(t *testing.T, enabled, withField bool) {
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.CSIStorageCapacity, enabled)()
driverName := "test-driver"
attachRequired := true
podInfoOnMount := true
requiresRepublish := true
storageCapacity := true
csiDriver := storage.CSIDriver{
ObjectMeta: metav1.ObjectMeta{Name: driverName},
Spec: storage.CSIDriverSpec{
AttachRequired: &attachRequired,
PodInfoOnMount: &podInfoOnMount,
RequiresRepublish: &requiresRepublish,
},
}
if withField {
csiDriver.Spec.StorageCapacity = &storageCapacity
}
errs := ValidateCSIDriver(&csiDriver)
success := !enabled || withField
if success && len(errs) != 0 {
t.Errorf("expected success, got: %v", errs)
}
if !success && len(errs) == 0 {
t.Errorf("expected error, got success")
}
}
yesNo := []bool{true, false}
for _, enabled := range yesNo {
t.Run(fmt.Sprintf("CSIStorageCapacity=%v", enabled), func(t *testing.T) {
for _, withField := range yesNo {
t.Run(fmt.Sprintf("with-field=%v", withField), func(t *testing.T) {
run(t, enabled, withField)
})
}
})
}
}
func TestValidateCSIStorageCapacity(t *testing.T) { func TestValidateCSIStorageCapacity(t *testing.T) {
storageClassName := "test-sc" storageClassName := "test-sc"
invalidName := "-invalid-@#$%^&*()-" invalidName := "-invalid-@#$%^&*()-"
@ -2179,6 +2261,7 @@ func TestCSIServiceAccountToken(t *testing.T) {
for _, test := range tests { for _, test := range tests {
test.csiDriver.Spec.AttachRequired = new(bool) test.csiDriver.Spec.AttachRequired = new(bool)
test.csiDriver.Spec.PodInfoOnMount = new(bool) test.csiDriver.Spec.PodInfoOnMount = new(bool)
test.csiDriver.Spec.StorageCapacity = new(bool)
if errs := ValidateCSIDriver(test.csiDriver); test.wantErr != (len(errs) != 0) { if errs := ValidateCSIDriver(test.csiDriver); test.wantErr != (len(errs) != 0) {
t.Errorf("ValidateCSIDriver = %v, want err: %v", errs, test.wantErr) t.Errorf("ValidateCSIDriver = %v, want err: %v", errs, test.wantErr)
} }

View File

@ -49,6 +49,7 @@ func validNewCSIDriver(name string) *storageapi.CSIDriver {
attachRequired := true attachRequired := true
podInfoOnMount := true podInfoOnMount := true
requiresRepublish := true requiresRepublish := true
storageCapacity := true
return &storageapi.CSIDriver{ return &storageapi.CSIDriver{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: name, Name: name,
@ -57,6 +58,7 @@ func validNewCSIDriver(name string) *storageapi.CSIDriver {
AttachRequired: &attachRequired, AttachRequired: &attachRequired,
PodInfoOnMount: &podInfoOnMount, PodInfoOnMount: &podInfoOnMount,
RequiresRepublish: &requiresRepublish, RequiresRepublish: &requiresRepublish,
StorageCapacity: &storageCapacity,
}, },
} }
} }
@ -71,6 +73,7 @@ func TestCreate(t *testing.T) {
attachNotRequired := false attachNotRequired := false
notPodInfoOnMount := false notPodInfoOnMount := false
notRequiresRepublish := false notRequiresRepublish := false
notStorageCapacity := false
test.TestCreate( test.TestCreate(
// valid // valid
csiDriver, csiDriver,
@ -81,6 +84,7 @@ func TestCreate(t *testing.T) {
AttachRequired: &attachNotRequired, AttachRequired: &attachNotRequired,
PodInfoOnMount: &notPodInfoOnMount, PodInfoOnMount: &notPodInfoOnMount,
RequiresRepublish: &notRequiresRepublish, RequiresRepublish: &notRequiresRepublish,
StorageCapacity: &notStorageCapacity,
}, },
}, },
) )

View File

@ -284,6 +284,14 @@ func GetEtcdStorageDataForNamespace(namespace string) map[schema.GroupVersionRes
}, },
// -- // --
// k8s.io/kubernetes/pkg/apis/storage/v1alpha1
gvr("storage.k8s.io", "v1alpha1", "csistoragecapacities"): {
Stub: `{"metadata": {"name": "csc-12345-1"}, "storageClassName": "sc1"}`,
ExpectedEtcdPath: "/registry/csistoragecapacities/" + namespace + "/csc-12345-1",
ExpectedGVK: gvkP("storage.k8s.io", "v1beta1", "CSIStorageCapacity"),
},
// --
// k8s.io/kubernetes/pkg/apis/flowcontrol/v1alpha1 // k8s.io/kubernetes/pkg/apis/flowcontrol/v1alpha1
gvr("flowcontrol.apiserver.k8s.io", "v1alpha1", "flowschemas"): { gvr("flowcontrol.apiserver.k8s.io", "v1alpha1", "flowschemas"): {
Stub: `{"metadata": {"name": "va1"}, "spec": {"priorityLevelConfiguration": {"name": "name1"}}}`, Stub: `{"metadata": {"name": "va1"}, "spec": {"priorityLevelConfiguration": {"name": "name1"}}}`,
@ -337,6 +345,13 @@ func GetEtcdStorageDataForNamespace(namespace string) map[schema.GroupVersionRes
}, },
// -- // --
// k8s.io/kubernetes/pkg/apis/storage/v1beta1
gvr("storage.k8s.io", "v1beta1", "csistoragecapacities"): {
Stub: `{"metadata": {"name": "csc-12345-2"}, "storageClassName": "sc1"}`,
ExpectedEtcdPath: "/registry/csistoragecapacities/" + namespace + "/csc-12345-2",
},
// --
// k8s.io/kubernetes/pkg/apis/storage/v1 // k8s.io/kubernetes/pkg/apis/storage/v1
gvr("storage.k8s.io", "v1", "storageclasses"): { gvr("storage.k8s.io", "v1", "storageclasses"): {
Stub: `{"metadata": {"name": "sc2"}, "provisioner": "aws"}`, Stub: `{"metadata": {"name": "sc2"}, "provisioner": "aws"}`,