From ebc59f6d54180b5cbc3e1d65ae6d6e70dae36f82 Mon Sep 17 00:00:00 2001 From: Grant Griffiths Date: Wed, 15 May 2019 16:40:57 -0700 Subject: [PATCH] Fix unit tests to use fake storage class object Signed-off-by: Grant Griffiths --- pkg/controller/controller.go | 1 - pkg/controller/controller_test.go | 80 +++++++++++++++++++++++++------ 2 files changed, 65 insertions(+), 16 deletions(-) diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index 71bb90354..71d2c7cf7 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -686,7 +686,6 @@ func (p *csiProvisioner) Delete(volume *v1.PersistentVolume) error { } req.Secrets = credentials } - } ctx, cancel := context.WithTimeout(context.Background(), p.timeout) defer cancel() diff --git a/pkg/controller/controller_test.go b/pkg/controller/controller_test.go index a233cc119..f3b30a846 100644 --- a/pkg/controller/controller_test.go +++ b/pkg/controller/controller_test.go @@ -37,6 +37,7 @@ import ( "google.golang.org/grpc" corev1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1" + storagev1 "k8s.io/api/storage/v1" storage "k8s.io/api/storage/v1beta1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -585,7 +586,12 @@ func TestGetSecretReference(t *testing.T) { params: map[string]string{ prefixedProvisionerSecretNameKey: "static-${pv.name}-${pvc.namespace}-${pvc.name}-${pvc.annotations['akey']}", }, - pvc: &v1.PersistentVolumeClaim{}, + pvc: &v1.PersistentVolumeClaim{ + ObjectMeta: metav1.ObjectMeta{ + Name: "name", + Namespace: "ns", + }, + }, expectErr: true, }, "template - valid": { @@ -603,7 +609,7 @@ func TestGetSecretReference(t *testing.T) { }, expectRef: &v1.SecretReference{Name: "static-pvname-pvcnamespace-pvcname", Namespace: "static-pvname-pvcnamespace"}, }, - "template - valid, with pvc name": { + "template - valid, with pvc.name": { secretParams: provisionerSecretParams, params: map[string]string{ provisionerSecretNameKey: "${pvc.name}", @@ -611,11 +617,11 @@ func TestGetSecretReference(t *testing.T) { }, pvc: &v1.PersistentVolumeClaim{ ObjectMeta: metav1.ObjectMeta{ - Name: "name", - Namespace: "ns", + Name: "pvcname", + Namespace: "pvcns", }, }, - expectRef: &v1.SecretReference{Name: "name", Namespace: "ns"}, + expectRef: &v1.SecretReference{Name: "pvcname", Namespace: "ns"}, }, "template - valid, provisioner with pvc name and namepsace": { secretParams: provisionerSecretParams, @@ -1850,8 +1856,9 @@ func TestProvisionWithMountOptions(t *testing.T) { type deleteTestcase struct { persistentVolume *v1.PersistentVolume - - expectErr bool + storageClass *storagev1.StorageClass + mockDelete bool + expectErr bool } // TestDelete is a test of the delete operation @@ -1876,14 +1883,41 @@ func TestDelete(t *testing.T) { }, expectErr: true, }, - "simple - valid": deleteTestcase{ + "fail - pvc.annotations not supported": deleteTestcase{ persistentVolume: &v1.PersistentVolume{ ObjectMeta: metav1.ObjectMeta{ Name: "pv", Namespace: "ns", - Annotations: map[string]string{ - prefixedProvisionerSecretNameKey: "static-${pv.name}-${pvc.namespace}-${pvc.name}-${pvc.annotations['akey']}", + }, + Spec: v1.PersistentVolumeSpec{ + PersistentVolumeSource: v1.PersistentVolumeSource{ + CSI: &v1.CSIPersistentVolumeSource{ + VolumeHandle: "vol-id-1", + }, + }, + ClaimRef: &v1.ObjectReference{ + Name: "sc-name", + Namespace: "ns", }, + StorageClassName: "sc-name", + }, + }, + storageClass: &storagev1.StorageClass{ + ObjectMeta: metav1.ObjectMeta{ + Name: "sc-name", + Namespace: "ns", + }, + Parameters: map[string]string{ + prefixedProvisionerSecretNameKey: "static-${pv.name}-${pvc.namespace}-${pvc.name}-${pvc.annotations['akey']}", + }, + }, + expectErr: true, + }, + "simple - valid case": deleteTestcase{ + persistentVolume: &v1.PersistentVolume{ + ObjectMeta: metav1.ObjectMeta{ + Name: "pv", + Namespace: "ns", }, Spec: v1.PersistentVolumeSpec{ PersistentVolumeSource: v1.PersistentVolumeSource{ @@ -1893,7 +1927,17 @@ func TestDelete(t *testing.T) { }, }, }, - expectErr: false, + storageClass: &storagev1.StorageClass{ + ObjectMeta: metav1.ObjectMeta{ + Name: "sc-name", + Namespace: "ns", + }, + Parameters: map[string]string{ + prefixedProvisionerSecretNameKey: "static-${pv.name}-${pvc.namespace}-${pvc.name}", + }, + }, + expectErr: false, + mockDelete: true, }, } @@ -1915,15 +1959,21 @@ func runDeleteTest(t *testing.T, k string, tc deleteTestcase) { defer mockController.Finish() defer driver.Stop() - clientSet := fakeclientset.NewSimpleClientset() + var clientSet *fakeclientset.Clientset - pluginCaps, controllerCaps := provisionCapabilities() - csiProvisioner := NewCSIProvisioner(clientSet, 5*time.Second, "test-provisioner", "test", 5, csiConn.conn, nil, driverName, pluginCaps, controllerCaps, "") + if tc.storageClass != nil { + clientSet = fakeclientset.NewSimpleClientset(tc.storageClass) + } else { + clientSet = fakeclientset.NewSimpleClientset() + } - if !tc.expectErr { + if tc.mockDelete { controllerServer.EXPECT().DeleteVolume(gomock.Any(), gomock.Any()).Return(&csi.DeleteVolumeResponse{}, nil).Times(1) } + pluginCaps, controllerCaps := provisionCapabilities() + csiProvisioner := NewCSIProvisioner(clientSet, 5*time.Second, "test-provisioner", "test", 5, csiConn.conn, nil, driverName, pluginCaps, controllerCaps, "") + err = csiProvisioner.Delete(tc.persistentVolume) if tc.expectErr && err == nil { t.Errorf("test %q: Expected error, got none", k)