|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/kubernetes-csi/external-resizer/pkg/csi" |
| 10 | + "github.com/kubernetes-csi/external-resizer/pkg/resizer" |
| 11 | + |
| 12 | + "k8s.io/api/core/v1" |
| 13 | + "k8s.io/apimachinery/pkg/api/resource" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/apimachinery/pkg/runtime" |
| 16 | + "k8s.io/client-go/informers" |
| 17 | + "k8s.io/client-go/kubernetes" |
| 18 | + "k8s.io/client-go/kubernetes/fake" |
| 19 | +) |
| 20 | + |
| 21 | +func TestController(t *testing.T) { |
| 22 | + for _, test := range []struct { |
| 23 | + Name string |
| 24 | + PVC *v1.PersistentVolumeClaim |
| 25 | + PV *v1.PersistentVolume |
| 26 | + |
| 27 | + CreateObjects bool |
| 28 | + NodeResize bool |
| 29 | + }{ |
| 30 | + { |
| 31 | + Name: "Invalid key", |
| 32 | + PVC: invalidPVC(), |
| 33 | + }, |
| 34 | + { |
| 35 | + Name: "PVC not found", |
| 36 | + PVC: createPVC(1, 1), |
| 37 | + }, |
| 38 | + { |
| 39 | + Name: "PVC doesn't need resize", |
| 40 | + PVC: createPVC(1, 1), |
| 41 | + CreateObjects: true, |
| 42 | + }, |
| 43 | + { |
| 44 | + Name: "PV not found", |
| 45 | + PVC: createPVC(2, 1), |
| 46 | + CreateObjects: true, |
| 47 | + }, |
| 48 | + { |
| 49 | + Name: "PV doesn't need resize", |
| 50 | + PVC: createPVC(2, 1), |
| 51 | + PV: createPV(2), |
| 52 | + CreateObjects: true, |
| 53 | + }, |
| 54 | + { |
| 55 | + Name: "Resize PVC, no FS resize", |
| 56 | + PVC: createPVC(2, 1), |
| 57 | + PV: createPV(1), |
| 58 | + CreateObjects: true, |
| 59 | + }, |
| 60 | + { |
| 61 | + Name: "Resize PVC with FS resize", |
| 62 | + PVC: createPVC(2, 1), |
| 63 | + PV: createPV(1), |
| 64 | + CreateObjects: true, |
| 65 | + NodeResize: true, |
| 66 | + }, |
| 67 | + } { |
| 68 | + client := csi.NewMockClient(test.NodeResize, true, true) |
| 69 | + driverName, _ := client.GetDriverName(context.TODO()) |
| 70 | + |
| 71 | + initialObjects := []runtime.Object{} |
| 72 | + if test.CreateObjects { |
| 73 | + if test.PVC != nil { |
| 74 | + initialObjects = append(initialObjects, test.PVC) |
| 75 | + } |
| 76 | + if test.PV != nil { |
| 77 | + test.PV.Spec.PersistentVolumeSource.CSI.Driver = driverName |
| 78 | + initialObjects = append(initialObjects, test.PV) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + kubeClient, informerFactory := fakeK8s(initialObjects) |
| 83 | + pvInformer := informerFactory.Core().V1().PersistentVolumes() |
| 84 | + pvcInformer := informerFactory.Core().V1().PersistentVolumeClaims() |
| 85 | + |
| 86 | + for _, obj := range initialObjects { |
| 87 | + switch obj.(type) { |
| 88 | + case *v1.PersistentVolume: |
| 89 | + pvInformer.Informer().GetStore().Add(obj) |
| 90 | + case *v1.PersistentVolumeClaim: |
| 91 | + pvcInformer.Informer().GetStore().Add(obj) |
| 92 | + default: |
| 93 | + t.Fatalf("Test %s: Unknown initalObject type: %+v", test.Name, obj) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + csiResizer, err := resizer.NewResizerFromClient(client, 15*time.Second, kubeClient, informerFactory) |
| 98 | + if err != nil { |
| 99 | + t.Fatalf("Test %s: Unable to create resizer: %v", test.Name, err) |
| 100 | + } |
| 101 | + |
| 102 | + controller := NewResizeController(driverName, csiResizer, kubeClient, time.Second, informerFactory) |
| 103 | + err = controller.(*resizeController).syncPVC(fmt.Sprintf("%s/%s", test.PVC.Namespace, test.PVC.Name)) |
| 104 | + if err != nil { |
| 105 | + t.Fatalf("Test %s: Unexpected error: %v", test.Name, err) |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func invalidPVC() *v1.PersistentVolumeClaim { |
| 111 | + pvc := createPVC(1, 1) |
| 112 | + pvc.ObjectMeta.Name = "" |
| 113 | + pvc.ObjectMeta.Namespace = "" |
| 114 | + |
| 115 | + return pvc |
| 116 | +} |
| 117 | + |
| 118 | +func quantityGB(i int) resource.Quantity { |
| 119 | + q := resource.NewQuantity(int64(i*1024*1024), resource.BinarySI) |
| 120 | + return *q |
| 121 | +} |
| 122 | + |
| 123 | +func createPVC(requestGB, capacityGB int) *v1.PersistentVolumeClaim { |
| 124 | + request := quantityGB(requestGB) |
| 125 | + capacity := quantityGB(capacityGB) |
| 126 | + |
| 127 | + return &v1.PersistentVolumeClaim{ |
| 128 | + ObjectMeta: metav1.ObjectMeta{ |
| 129 | + Name: "testPVC", |
| 130 | + Namespace: "test", |
| 131 | + }, |
| 132 | + Spec: v1.PersistentVolumeClaimSpec{ |
| 133 | + Resources: v1.ResourceRequirements{ |
| 134 | + Requests: map[v1.ResourceName]resource.Quantity{ |
| 135 | + v1.ResourceStorage: request, |
| 136 | + }, |
| 137 | + }, |
| 138 | + VolumeName: "testPV", |
| 139 | + }, |
| 140 | + Status: v1.PersistentVolumeClaimStatus{ |
| 141 | + Phase: v1.ClaimBound, |
| 142 | + Capacity: map[v1.ResourceName]resource.Quantity{ |
| 143 | + v1.ResourceStorage: capacity, |
| 144 | + }, |
| 145 | + }, |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func createPV(capacityGB int) *v1.PersistentVolume { |
| 150 | + capacity := quantityGB(capacityGB) |
| 151 | + |
| 152 | + return &v1.PersistentVolume{ |
| 153 | + ObjectMeta: metav1.ObjectMeta{ |
| 154 | + Name: "testPV", |
| 155 | + }, |
| 156 | + Spec: v1.PersistentVolumeSpec{ |
| 157 | + Capacity: map[v1.ResourceName]resource.Quantity{ |
| 158 | + v1.ResourceStorage: capacity, |
| 159 | + }, |
| 160 | + PersistentVolumeSource: v1.PersistentVolumeSource{ |
| 161 | + CSI: &v1.CSIPersistentVolumeSource{ |
| 162 | + Driver: "foo", |
| 163 | + VolumeHandle: "foo", |
| 164 | + }, |
| 165 | + }, |
| 166 | + }, |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +func fakeK8s(objs []runtime.Object) (kubernetes.Interface, informers.SharedInformerFactory) { |
| 171 | + client := fake.NewSimpleClientset(objs...) |
| 172 | + informerFactory := informers.NewSharedInformerFactory(client, 0) |
| 173 | + return client, informerFactory |
| 174 | +} |
0 commit comments