github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/integration_tests/sanity/customresources_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 sanity 18 19 import ( 20 . "github.com/onsi/ginkgo" 21 . "github.com/onsi/gomega" 22 "github.com/openebs/node-disk-manager/api/v1alpha1" 23 "github.com/openebs/node-disk-manager/integration_tests/k8s" 24 "github.com/openebs/node-disk-manager/integration_tests/udev" 25 "strings" 26 ) 27 28 var _ = Describe("Device Discovery Tests", func() { 29 30 var ( 31 err error 32 k8sClient k8s.K8sClient 33 ) 34 35 k8sClient, _ = k8s.GetClientSet() 36 BeforeEach(func() { 37 By("getting a new client set") 38 _ = k8sClient.RegenerateClient() 39 By("creating the NDM Daemonset") 40 err = k8sClient.CreateNDMDaemonSet() 41 Expect(err).NotTo(HaveOccurred()) 42 43 By("waiting for the daemonset pod to be running") 44 ok := WaitForPodToBeRunningEventually(DaemonSetPodPrefix) 45 Expect(ok).To(BeTrue()) 46 47 }) 48 AfterEach(func() { 49 By("deleting the NDM deamonset") 50 err := k8sClient.DeleteNDMDaemonSet() 51 Expect(err).NotTo(HaveOccurred()) 52 53 By("waiting for the pod to be removed") 54 ok := WaitForPodToBeDeletedEventually(DaemonSetPodPrefix) 55 Expect(ok).To(BeTrue()) 56 }) 57 58 Context("Setup with no external disk", func() { 59 60 It("should have one sparse block device", func() { 61 By("regenerating the client set") 62 _ = k8sClient.RegenerateClient() 63 64 By("listing all blockdevices") 65 bdList, err := k8sClient.ListBlockDevices() 66 Expect(err).NotTo(HaveOccurred()) 67 68 noOfSparseBlockDevices := 0 69 // Get the no.of sparse block devices from block device list 70 By("counting the number of sparse BDs") 71 for _, blockDevice := range bdList.Items { 72 if strings.Contains(blockDevice.Name, SparseBlockDeviceName) { 73 Expect(blockDevice.Status.State).To(Equal(v1alpha1.BlockDeviceActive)) 74 noOfSparseBlockDevices++ 75 } 76 } 77 Expect(noOfSparseBlockDevices).To(Equal(1)) 78 }) 79 }) 80 81 Context("Setup with a single external disk already attached", func() { 82 var physicalDisk udev.Disk 83 By("creating and attaching a new disk") 84 physicalDisk = udev.NewDisk(DiskImageSize) 85 _ = physicalDisk.AttachDisk() 86 87 It("should have 2 BlockDeviceCR", func() { 88 By("regenerating the client set") 89 _ = k8sClient.RegenerateClient() 90 91 // should have 2 block device CR, one for sparse disk and one for the 92 // external disk 93 By("listing all the BlockDevices") 94 bdList, err := k8sClient.ListBlockDevices() 95 Expect(err).NotTo(HaveOccurred()) 96 97 noOfSparseBlockDeviceCR := 0 98 noOfPhysicalBlockDeviceCR := 0 99 100 // Get no.of sparse blockdevices and physical blockdevices from bdList 101 By("counting the number of active sparse and blockdevices") 102 for _, blockDevice := range bdList.Items { 103 if strings.Contains(blockDevice.Name, BlockDeviceName) && blockDevice.Spec.Path == physicalDisk.Name { 104 noOfPhysicalBlockDeviceCR++ 105 Expect(blockDevice.Status.State).To(Equal(v1alpha1.BlockDeviceActive)) 106 } else if strings.Contains(blockDevice.Name, SparseBlockDeviceName) { 107 noOfSparseBlockDeviceCR++ 108 Expect(blockDevice.Status.State).To(Equal(v1alpha1.BlockDeviceActive)) 109 } 110 } 111 112 Expect(noOfSparseBlockDeviceCR).To(Equal(1)) 113 Expect(noOfPhysicalBlockDeviceCR).To(Equal(1)) 114 }) 115 It("should have blockdeviceCR inactive when disk is detached", func() { 116 By("regenerating the client set") 117 _ = k8sClient.RegenerateClient() 118 119 By("detaching the disk") 120 err = physicalDisk.DetachDisk() 121 k8s.WaitForReconciliation() 122 123 By("listing all the blockdevices") 124 bdList, err := k8sClient.ListBlockDevices() 125 Expect(err).NotTo(HaveOccurred()) 126 127 noOfPhysicalBlockDeviceCR := 0 128 129 By("checking for inactive blockdevice") 130 for _, bd := range bdList.Items { 131 if strings.Contains(bd.Name, BlockDeviceName) && bd.Spec.Path == physicalDisk.Name { 132 noOfPhysicalBlockDeviceCR++ 133 Expect(bd.Status.State).To(Equal(v1alpha1.BlockDeviceInactive)) 134 } 135 } 136 137 By("verifying only block device was made inactive") 138 Expect(noOfPhysicalBlockDeviceCR).To(Equal(1)) 139 }) 140 }) 141 Context("Setup with a single external disk attached at runtime", func() { 142 var physicalDisk udev.Disk 143 physicalDisk = udev.NewDisk(DiskImageSize) 144 145 It("should have one additional BlockDevice CR after we attach a disk", func() { 146 By("regenerating the client set") 147 _ = k8sClient.RegenerateClient() 148 149 By("listing all the blockdevices") 150 bdList, err := k8sClient.ListBlockDevices() 151 Expect(err).NotTo(HaveOccurred()) 152 noOfBlockDeviceCR := len(bdList.Items) 153 154 By("attaching the disk") 155 err = physicalDisk.AttachDisk() 156 Expect(err).NotTo(HaveOccurred()) 157 k8s.WaitForReconciliation() 158 159 By("listing the blockdevices again") 160 bdList, err = k8sClient.ListBlockDevices() 161 Expect(err).NotTo(HaveOccurred()) 162 163 By("checking count of blockdevices") 164 Expect(len(bdList.Items)).To(Equal(noOfBlockDeviceCR + 1)) 165 }) 166 }) 167 })