github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/integration_tests/sanity/deviceclaim_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  	apis "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  	"k8s.io/apimachinery/pkg/api/resource"
    26  	"os"
    27  )
    28  
    29  const (
    30  	// FakeHostName is a generated fake hostname
    31  	FakeHostName = "fake-minikube"
    32  	// FakeBlockDevice is a generated fake block device name
    33  	FakeBlockDevice = "fake-BD"
    34  )
    35  
    36  var (
    37  	BDCUnavailableCapacity = resource.MustParse("10Gi")
    38  	BDCAvailableCapacity   = resource.MustParse("1Gi")
    39  	// HostName is the hostname in which the tests are performed
    40  	HostName = os.Getenv("HOSTNAME")
    41  )
    42  
    43  var _ = Describe("BlockDevice Claim tests", func() {
    44  
    45  	var err error
    46  	var k8sClient k8s.K8sClient
    47  	physicalDisk := udev.NewDisk(DiskImageSize)
    48  	_ = physicalDisk.AttachDisk()
    49  
    50  	BeforeEach(func() {
    51  		By("getting a new client set")
    52  		k8sClient, err = k8s.GetClientSet()
    53  		Expect(err).NotTo(HaveOccurred())
    54  
    55  		By("creating the NDM Daemonset")
    56  		err = k8sClient.CreateNDMDaemonSet()
    57  		Expect(err).NotTo(HaveOccurred())
    58  
    59  		By("waiting for the daemonset pod to be running")
    60  		ok := WaitForPodToBeRunningEventually(DaemonSetPodPrefix)
    61  		Expect(ok).To(BeTrue())
    62  
    63  		k8s.WaitForReconciliation()
    64  	})
    65  	AfterEach(func() {
    66  		By("deleting the NDM deamonset")
    67  		err := k8sClient.DeleteNDMDaemonSet()
    68  		Expect(err).NotTo(HaveOccurred())
    69  
    70  		By("waiting for the pod to be removed")
    71  		ok := WaitForPodToBeDeletedEventually(DaemonSetPodPrefix)
    72  		Expect(ok).To(BeTrue())
    73  	})
    74  	Context("Claim Block Device when matching BD is not available", func() {
    75  		var bdcName string
    76  		var blockDeviceClaim *apis.BlockDeviceClaim
    77  		BeforeEach(func() {
    78  			By("building a new BDC")
    79  			bdcName = "test-bdc-1"
    80  			blockDeviceClaim = k8s.NewBDC(bdcName)
    81  		})
    82  		AfterEach(func() {
    83  			// delete the BDC
    84  			By("deleting the BDC as part of cleanup")
    85  			err = k8sClient.DeleteBlockDeviceClaim(blockDeviceClaim)
    86  			Expect(err).NotTo(HaveOccurred())
    87  		})
    88  		It("BD is not available on the host", func() {
    89  			blockDeviceClaim.Spec.HostName = FakeHostName
    90  			blockDeviceClaim.Namespace = k8s.Namespace
    91  			blockDeviceClaim.Spec.Resources.Requests[apis.ResourceStorage] = BDCAvailableCapacity
    92  
    93  			By("creating BDC object")
    94  			err = k8sClient.CreateBlockDeviceClaim(blockDeviceClaim)
    95  			Expect(err).NotTo(HaveOccurred())
    96  			k8s.WaitForReconciliation()
    97  
    98  			By("listing all BDCs")
    99  			bdcList, err := k8sClient.ListBlockDeviceClaims()
   100  			Expect(err).NotTo(HaveOccurred())
   101  			Expect(len(bdcList.Items)).NotTo(BeZero())
   102  
   103  			By("check whether BDC is in Pending state")
   104  			for _, bdc := range bdcList.Items {
   105  				if bdc.Name == bdcName {
   106  					Expect(bdc.Status.Phase).To(Equal(apis.BlockDeviceClaimStatusPending))
   107  				}
   108  			}
   109  		})
   110  
   111  		It("BD with resource requirement is not available on the host", func() {
   112  			blockDeviceClaim.Spec.HostName = HostName
   113  			blockDeviceClaim.Namespace = k8s.Namespace
   114  			blockDeviceClaim.Spec.Resources.Requests[apis.ResourceStorage] = BDCUnavailableCapacity
   115  
   116  			By("creating BDC object with unavailable capacity")
   117  			err = k8sClient.CreateBlockDeviceClaim(blockDeviceClaim)
   118  			Expect(err).NotTo(HaveOccurred())
   119  			k8s.WaitForReconciliation()
   120  
   121  			By("listing all BDCs")
   122  			bdcList, err := k8sClient.ListBlockDeviceClaims()
   123  			Expect(err).NotTo(HaveOccurred())
   124  			Expect(len(bdcList.Items)).NotTo(BeZero())
   125  
   126  			By("check whether BDC is in Pending state")
   127  			for _, bdc := range bdcList.Items {
   128  				if bdc.Name == bdcName {
   129  					Expect(bdc.Status.Phase).To(Equal(apis.BlockDeviceClaimStatusPending))
   130  				}
   131  			}
   132  		})
   133  	})
   134  
   135  	Context("Claim Block Device when matching BD is available", func() {
   136  		var bdcName string
   137  		var blockDeviceClaim *apis.BlockDeviceClaim
   138  		BeforeEach(func() {
   139  			By("building a new BDC")
   140  			bdcName = "test-bdc-1"
   141  			blockDeviceClaim = k8s.NewBDC(bdcName)
   142  		})
   143  		AfterEach(func() {
   144  			By("getting the BDC from etcd")
   145  			blockDeviceClaim, err = k8sClient.GetBlockDeviceClaim(blockDeviceClaim.Namespace, blockDeviceClaim.Name)
   146  			Expect(err).NotTo(HaveOccurred())
   147  
   148  			By("remove finalizer on the BDC")
   149  			blockDeviceClaim.Finalizers = nil
   150  			err = k8sClient.UpdateBlockDeviceClaim(blockDeviceClaim)
   151  			Expect(err).NotTo(HaveOccurred())
   152  
   153  			// delete the BDC
   154  			By("deleting the BDC as part of cleanup")
   155  			err = k8sClient.DeleteBlockDeviceClaim(blockDeviceClaim)
   156  			Expect(err).NotTo(HaveOccurred())
   157  
   158  		})
   159  		It("has matching BD on the node", func() {
   160  			blockDeviceClaim.Spec.HostName = HostName
   161  			blockDeviceClaim.Namespace = k8s.Namespace
   162  			blockDeviceClaim.Spec.Resources.Requests[apis.ResourceStorage] = BDCAvailableCapacity
   163  
   164  			By("creating BDC with matching node")
   165  			err = k8sClient.CreateBlockDeviceClaim(blockDeviceClaim)
   166  			Expect(err).NotTo(HaveOccurred())
   167  			k8s.WaitForReconciliation()
   168  
   169  			By("listing all BDCs")
   170  			bdcList, err := k8sClient.ListBlockDeviceClaims()
   171  			Expect(err).NotTo(HaveOccurred())
   172  			Expect(len(bdcList.Items)).NotTo(BeZero())
   173  
   174  			var bdName string
   175  			// check status of BDC
   176  			By("checking if BDC is bound")
   177  			for _, bdc := range bdcList.Items {
   178  				if bdc.Name == bdcName {
   179  					bdName = bdc.Spec.BlockDeviceName
   180  					Expect(bdc.Status.Phase).To(Equal(apis.BlockDeviceClaimStatusDone))
   181  				}
   182  			}
   183  
   184  			// check status of BD that has been bound
   185  			By("listing all blockdevices")
   186  			bdList, err := k8sClient.ListBlockDevices()
   187  			Expect(err).NotTo(HaveOccurred())
   188  
   189  			By("checking if the corresponding BD is claimed")
   190  			for _, bd := range bdList.Items {
   191  				if bd.Name == bdName {
   192  					Expect(bd.Status.ClaimState).To(Equal(apis.BlockDeviceClaimed))
   193  				}
   194  			}
   195  
   196  		})
   197  	})
   198  	Context("Unclaiming a block device ", func() {
   199  		var bdcName string
   200  		var blockDeviceClaim *apis.BlockDeviceClaim
   201  		BeforeEach(func() {
   202  			By("building a new BDC")
   203  			bdcName = "test-bdc-1"
   204  			blockDeviceClaim = k8s.NewBDC(bdcName)
   205  		})
   206  		It("unclaims a BD when BDC is deleted", func() {
   207  			blockDeviceClaim.Spec.HostName = HostName
   208  			blockDeviceClaim.Namespace = k8s.Namespace
   209  			blockDeviceClaim.Spec.Resources.Requests[apis.ResourceStorage] = BDCAvailableCapacity
   210  
   211  			By("creating BDC object")
   212  			err := k8sClient.CreateBlockDeviceClaim(blockDeviceClaim)
   213  			Expect(err).NotTo(HaveOccurred())
   214  			k8s.WaitForReconciliation()
   215  
   216  			By("listing all BDCs")
   217  			bdcList, err := k8sClient.ListBlockDeviceClaims()
   218  			Expect(err).NotTo(HaveOccurred())
   219  			Expect(len(bdcList.Items)).NotTo(BeZero())
   220  
   221  			var bdName string
   222  			// check status of BDC
   223  			By("checking if BDC is bound")
   224  			for _, bdc := range bdcList.Items {
   225  				if bdc.Name == bdcName {
   226  					bdName = bdc.Spec.BlockDeviceName
   227  					Expect(bdc.Status.Phase).To(Equal(apis.BlockDeviceClaimStatusDone))
   228  				}
   229  			}
   230  
   231  			// check status of BD that has been bound
   232  			By("listing all blockdevices")
   233  			bdList, err := k8sClient.ListBlockDevices()
   234  			Expect(err).NotTo(HaveOccurred())
   235  
   236  			By("checking if the corresponding BD is claimed")
   237  			for _, bd := range bdList.Items {
   238  				if bd.Name == bdName {
   239  					Expect(bd.Status.ClaimState).To(Equal(apis.BlockDeviceClaimed))
   240  				}
   241  			}
   242  
   243  			By("deleting the BDC")
   244  			err = k8sClient.DeleteBlockDeviceClaim(blockDeviceClaim)
   245  			Expect(err).NotTo(HaveOccurred())
   246  			k8s.WaitForReconciliation()
   247  
   248  			// check status of BD again to check it has been released
   249  			bdList, err = k8sClient.ListBlockDevices()
   250  			Expect(err).NotTo(HaveOccurred())
   251  			By("checking if the corresponding BD is in released/unclaimed state")
   252  			for _, bd := range bdList.Items {
   253  				if bd.Name == bdName {
   254  					// BlockDevice can be in either released or unclaimed
   255  					// depending on the time required for cleanup
   256  					Expect(bd.Status.ClaimState).To(Or(Equal(apis.BlockDeviceReleased),
   257  						Equal(apis.BlockDeviceUnclaimed)))
   258  				}
   259  			}
   260  
   261  		})
   262  	})
   263  
   264  })