From 1f0d37c0828a6adc1897cad54461ed30baf73b50 Mon Sep 17 00:00:00 2001 From: Humble Chirammal Date: Mon, 28 Feb 2022 18:33:33 +0530 Subject: [PATCH] csi: add unit tests for {controller,node}Publish name & namespace Extra test conditions are added in CSIPersistentVolumeSource validation for controllerPublishSecretRef and nodePublishSecretRef name and namespace to check whether name field or namespace field is missing from the secretRef while validating CSI PersistentVolumeSource Signed-off-by: Humble Chirammal --- pkg/apis/core/validation/validation.go | 4 +-- pkg/apis/core/validation/validation_test.go | 34 ++++++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/pkg/apis/core/validation/validation.go b/pkg/apis/core/validation/validation.go index dbb49be1ca8..baa2e1540dd 100644 --- a/pkg/apis/core/validation/validation.go +++ b/pkg/apis/core/validation/validation.go @@ -1588,12 +1588,12 @@ func validateCSIPersistentVolumeSource(csi *core.CSIPersistentVolumeSource, fldP if csi.NodePublishSecretRef != nil { if len(csi.NodePublishSecretRef.Name) == 0 { - allErrs = append(allErrs, field.Required(fldPath.Child("nodePublishSecretRef ", "name"), "")) + allErrs = append(allErrs, field.Required(fldPath.Child("nodePublishSecretRef", "name"), "")) } else { allErrs = append(allErrs, ValidateDNS1123Label(csi.NodePublishSecretRef.Name, fldPath.Child("name"))...) } if len(csi.NodePublishSecretRef.Namespace) == 0 { - allErrs = append(allErrs, field.Required(fldPath.Child("nodePublishSecretRef ", "namespace"), "")) + allErrs = append(allErrs, field.Required(fldPath.Child("nodePublishSecretRef", "namespace"), "")) } else { allErrs = append(allErrs, ValidateDNS1123Label(csi.NodePublishSecretRef.Namespace, fldPath.Child("namespace"))...) } diff --git a/pkg/apis/core/validation/validation_test.go b/pkg/apis/core/validation/validation_test.go index 9144cadb982..c4c1baac006 100644 --- a/pkg/apis/core/validation/validation_test.go +++ b/pkg/apis/core/validation/validation_test.go @@ -2531,7 +2531,7 @@ func TestValidateGlusterfsPersistentVolumeSource(t *testing.T) { } } -func TestValidateCSIVolumeSource(t *testing.T) { +func TestValidateCSIPersistentVolumeSource(t *testing.T) { testCases := []struct { name string csi *core.CSIPersistentVolumeSource @@ -2646,6 +2646,38 @@ func TestValidateCSIVolumeSource(t *testing.T) { name: "valid controllerExpandSecretRef", csi: &core.CSIPersistentVolumeSource{Driver: "com.google.gcepd", VolumeHandle: "foobar", ControllerExpandSecretRef: &core.SecretReference{Name: "foobar", Namespace: "default"}}, }, + { + name: "controllerPublishSecretRef: invalid name missing", + csi: &core.CSIPersistentVolumeSource{Driver: "com.google.gcepd", VolumeHandle: "foobar", ControllerPublishSecretRef: &core.SecretReference{Namespace: "default"}}, + errtype: field.ErrorTypeRequired, + errfield: "controllerPublishSecretRef.name", + }, + { + name: "controllerPublishSecretRef: invalid namespace missing", + csi: &core.CSIPersistentVolumeSource{Driver: "com.google.gcepd", VolumeHandle: "foobar", ControllerPublishSecretRef: &core.SecretReference{Name: "foobar"}}, + errtype: field.ErrorTypeRequired, + errfield: "controllerPublishSecretRef.namespace", + }, + { + name: "valid controllerPublishSecretRef", + csi: &core.CSIPersistentVolumeSource{Driver: "com.google.gcepd", VolumeHandle: "foobar", ControllerPublishSecretRef: &core.SecretReference{Name: "foobar", Namespace: "default"}}, + }, + { + name: "valid nodePublishSecretRef", + csi: &core.CSIPersistentVolumeSource{Driver: "com.google.gcepd", VolumeHandle: "foobar", NodePublishSecretRef: &core.SecretReference{Name: "foobar", Namespace: "default"}}, + }, + { + name: "nodePublishSecretRef: invalid name missing", + csi: &core.CSIPersistentVolumeSource{Driver: "com.google.gcepd", VolumeHandle: "foobar", NodePublishSecretRef: &core.SecretReference{Namespace: "foobar"}}, + errtype: field.ErrorTypeRequired, + errfield: "nodePublishSecretRef.name", + }, + { + name: "nodePublishSecretRef: invalid namespace missing", + csi: &core.CSIPersistentVolumeSource{Driver: "com.google.gcepd", VolumeHandle: "foobar", NodePublishSecretRef: &core.SecretReference{Name: "foobar"}}, + errtype: field.ErrorTypeRequired, + errfield: "nodePublishSecretRef.namespace", + }, } for i, tc := range testCases {