github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/controllers/blockdevice/blockdevice_controller_test.go (about) 1 /* 2 Copyright 2019 The OpenEBS Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package blockdevice 18 19 import ( 20 "context" 21 //"math/rand" 22 //"reflect" 23 "fmt" 24 "testing" 25 26 openebsv1alpha1 "github.com/openebs/node-disk-manager/api/v1alpha1" 27 ndm "github.com/openebs/node-disk-manager/cmd/ndm_daemonset/controller" 28 29 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 30 "k8s.io/apimachinery/pkg/runtime" 31 "k8s.io/apimachinery/pkg/types" 32 "k8s.io/client-go/kubernetes/scheme" 33 "k8s.io/client-go/tools/record" 34 "sigs.k8s.io/controller-runtime/pkg/client" 35 "sigs.k8s.io/controller-runtime/pkg/client/fake" 36 logf "sigs.k8s.io/controller-runtime/pkg/log" 37 "sigs.k8s.io/controller-runtime/pkg/log/zap" 38 "sigs.k8s.io/controller-runtime/pkg/reconcile" 39 ) 40 41 var ( 42 fakeHostName = "fake-hostname" 43 diskName = "disk-example" 44 deviceName = "blockdevice-example" 45 namespace = "" 46 capacity uint64 = 1024000 47 fakeRecorder = record.NewFakeRecorder(50) 48 ) 49 50 // TestDeviceController runs ReconcileDisk.Reconcile() against a 51 // fake client that tracks a BlockDevice object. 52 // Test description: 53 // Create a disk obj and associated blockdevice obj, check status of blockdevice obj, 54 // it should be Active, now mark disk Inactive and trigger reconcile logic 55 // on blockdevice, it would mark blockdevice Inactive as well. Check status of blockdevice, 56 // this time it should be Inactive. 57 func TestDeviceController(t *testing.T) { 58 59 // Set the logger to development mode for verbose logs. 60 logf.SetLogger(zap.New(zap.UseDevMode(true))) 61 62 // Create a fake client to mock API calls. 63 cl, s := CreateFakeClient(t) 64 65 // Create a ReconcileBlockDevice object with the scheme and fake client. 66 r := &BlockDeviceReconciler{Client: cl, Scheme: s, Recorder: fakeRecorder} 67 68 // Mock request to simulate Reconcile() being called on an event for a 69 // watched resource . 70 req := reconcile.Request{ 71 NamespacedName: types.NamespacedName{ 72 Name: deviceName, 73 Namespace: namespace, 74 }, 75 } 76 77 res, err := r.Reconcile(context.TODO(), req) 78 if err != nil { 79 t.Fatalf("reconcile: (%v)", err) 80 } 81 82 // Check the result of reconciliation to make sure it has the desired state. 83 if !res.Requeue { 84 t.Log("reconcile did not requeue request as expected") 85 } 86 87 deviceInstance := &openebsv1alpha1.BlockDevice{} 88 err = r.Client.Get(context.TODO(), req.NamespacedName, deviceInstance) 89 if err != nil { 90 t.Errorf("get deviceInstance : (%v)", err) 91 } 92 93 // Disk Status state should be Active as expected. 94 if deviceInstance.Status.State == ndm.NDMActive { 95 t.Logf("BlockDevice Object state:%v match expected state:%v", deviceInstance.Status.State, ndm.NDMActive) 96 } else { 97 t.Fatalf("BlockDevice Object state:%v did not match expected state:%v", deviceInstance.Status.State, ndm.NDMActive) 98 } 99 } 100 101 func GetFakeDeviceObject() *openebsv1alpha1.BlockDevice { 102 device := &openebsv1alpha1.BlockDevice{} 103 labels := map[string]string{ndm.NDMManagedKey: ndm.TrueString} 104 105 TypeMeta := metav1.TypeMeta{ 106 Kind: ndm.NDMBlockDeviceKind, 107 APIVersion: ndm.NDMVersion, 108 } 109 ObjectMeta := metav1.ObjectMeta{ 110 Labels: labels, 111 Name: deviceName, 112 Namespace: namespace, 113 } 114 115 Spec := openebsv1alpha1.DeviceSpec{ 116 Path: "dev/disk-fake-path", 117 Capacity: openebsv1alpha1.DeviceCapacity{ 118 Storage: capacity, // Set blockdevice size. 119 }, 120 DevLinks: make([]openebsv1alpha1.DeviceDevLink, 0), 121 Partitioned: ndm.NDMNotPartitioned, 122 } 123 124 device.ObjectMeta = ObjectMeta 125 device.TypeMeta = TypeMeta 126 device.Status.ClaimState = openebsv1alpha1.BlockDeviceUnclaimed 127 device.Status.State = ndm.NDMActive 128 device.Spec = Spec 129 return device 130 } 131 132 func CreateFakeClient(t *testing.T) (client.Client, *runtime.Scheme) { 133 134 deviceR := GetFakeDeviceObject() 135 136 deviceList := &openebsv1alpha1.BlockDeviceList{ 137 TypeMeta: metav1.TypeMeta{ 138 Kind: "BlockDevice", 139 APIVersion: "", 140 }, 141 } 142 143 s := scheme.Scheme 144 145 s.AddKnownTypes(openebsv1alpha1.GroupVersion, deviceR) 146 s.AddKnownTypes(openebsv1alpha1.GroupVersion, deviceList) 147 148 fakeNdmClient := fake.NewFakeClient() 149 if fakeNdmClient == nil { 150 fmt.Println("NDMClient is not created") 151 } 152 err := fakeNdmClient.Create(context.TODO(), deviceR) 153 if err != nil { 154 fmt.Println("BlockDevice object is not created") 155 } 156 return fakeNdmClient, s 157 }