github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/integration_tests/sanity/mount_change_test.go (about)

     1  /*
     2  Copyright 2020 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  	"io/ioutil"
    21  
    22  	. "github.com/onsi/ginkgo"
    23  	. "github.com/onsi/gomega"
    24  	"github.com/onsi/gomega/types"
    25  
    26  	"github.com/openebs/node-disk-manager/integration_tests/k8s"
    27  	"github.com/openebs/node-disk-manager/integration_tests/udev"
    28  )
    29  
    30  var _ = Describe("Mount-point and fs type change detection tests", func() {
    31  	var k8sClient k8s.K8sClient
    32  	physicalDisk := udev.NewDisk(DiskImageSize)
    33  
    34  	var bdName, bdNamespace, mountPath string
    35  
    36  	BeforeEach(setUp(&k8sClient, &physicalDisk, &bdName, &bdNamespace, &mountPath))
    37  	AfterEach(tearDown(&k8sClient, &physicalDisk))
    38  
    39  	It("should pass mount-umount flow", func() {
    40  		// Mount flow
    41  		By("Running mount flow")
    42  		By("Mounting disk", mountDisk(&physicalDisk, mountPath))
    43  		By("Waiting for etcd update", k8s.WaitForStateChange)
    44  		By("Verifying bd mount point update in etcd",
    45  			verifyMountPointUpdate(&k8sClient, bdName, bdNamespace,
    46  				Equal(mountPath)))
    47  
    48  		// Umount flow
    49  		By("Running umount flow")
    50  		By("Umounting disk", umountDisk(&physicalDisk))
    51  		By("Waiting for etcd update", k8s.WaitForStateChange)
    52  		By("Verifying bd mount point update in etcd",
    53  			verifyMountPointUpdate(&k8sClient, bdName, bdNamespace,
    54  				BeEmpty()))
    55  	})
    56  })
    57  
    58  func setUp(kcli *k8s.K8sClient, disk *udev.Disk, bdName, bdNamespace, mountPath *string) func() {
    59  	return func() {
    60  		By("initializing up k8s cli", initK8sCli(kcli))
    61  		By("creating up ndm daemonset", createAndStartNDMDaemonset(kcli))
    62  		By("setting up fs on disk", setupPhysicalDisk(disk))
    63  		By("attaching disk", attachDisk(disk))
    64  		By("generating random mount path", generateMountPath(mountPath))
    65  		By("waiting for etcd update", k8s.WaitForStateChange)
    66  		By("verifying disk added to etcd", verifyDiskAddedToEtcd(kcli, disk.Name,
    67  			bdName, bdNamespace))
    68  	}
    69  }
    70  
    71  func tearDown(kcli *k8s.K8sClient, disk *udev.Disk) func() {
    72  	return func() {
    73  		By("detaching disk", detachDisk(disk))
    74  		By("destroying ndm daemonset", stopAndDeleteNDMDaemonset(kcli))
    75  	}
    76  
    77  }
    78  
    79  func generateMountPath(mountPath *string) func() {
    80  	return func() {
    81  		mp, err := ioutil.TempDir("", "ndm-integration-tests")
    82  		Expect(err).ToNot(HaveOccurred())
    83  		*mountPath = mp
    84  	}
    85  }
    86  
    87  func initK8sCli(cli *k8s.K8sClient) func() {
    88  	return func() {
    89  		kcli, err := k8s.GetClientSet()
    90  		Expect(err).ToNot(HaveOccurred())
    91  		*cli = kcli
    92  	}
    93  }
    94  
    95  func setupPhysicalDisk(disk *udev.Disk) func() {
    96  	return func() {
    97  		Expect(disk.CreateFileSystem()).ToNot(HaveOccurred())
    98  	}
    99  }
   100  
   101  func createAndStartNDMDaemonset(cli *k8s.K8sClient) func() {
   102  	return func() {
   103  		err := cli.CreateNDMDaemonSet()
   104  		Expect(err).ToNot(HaveOccurred())
   105  
   106  		ok := WaitForPodToBeRunningEventually(DaemonSetPodPrefix)
   107  		Expect(ok).To(BeTrue())
   108  	}
   109  }
   110  
   111  func stopAndDeleteNDMDaemonset(cli *k8s.K8sClient) func() {
   112  	return func() {
   113  		k8s.WaitForReconciliation()
   114  
   115  		err := cli.DeleteNDMDaemonSet()
   116  		Expect(err).ToNot(HaveOccurred())
   117  
   118  		ok := WaitForPodToBeDeletedEventually(DaemonSetPodPrefix)
   119  		Expect(ok).To(BeTrue())
   120  	}
   121  }
   122  
   123  func verifyMountPointUpdate(cli *k8s.K8sClient, bdName, bdNamespace string,
   124  	matcher types.GomegaMatcher) func() {
   125  	return func() {
   126  		bd, err := cli.GetBlockDevice(bdName, bdNamespace)
   127  		Expect(err).ToNot(HaveOccurred())
   128  		Expect(bd).ToNot(BeNil())
   129  		Expect(bd.Name).To(Equal(bdName))
   130  		Expect(bd.Namespace).To(Equal(bdNamespace))
   131  		Expect(bd.Spec.FileSystem.Mountpoint).To(matcher)
   132  	}
   133  }
   134  
   135  func mountDisk(disk *udev.Disk, mountPath string) func() {
   136  	return func() {
   137  		Expect(disk.Mount(mountPath)).ToNot(HaveOccurred())
   138  	}
   139  }
   140  
   141  func umountDisk(disk *udev.Disk) func() {
   142  	return func() {
   143  		Expect(disk.Unmount()).ToNot(HaveOccurred())
   144  	}
   145  }
   146  
   147  func attachDisk(disk *udev.Disk) func() {
   148  	return func() {
   149  		Expect(disk.AttachDisk()).ToNot(HaveOccurred())
   150  	}
   151  }
   152  
   153  func detachDisk(disk *udev.Disk) func() {
   154  	return func() {
   155  		Expect(disk.DetachAndDeleteDisk()).ToNot(HaveOccurred())
   156  	}
   157  }
   158  
   159  func verifyDiskAddedToEtcd(cli *k8s.K8sClient, diskName string, bdName *string, bdNamespace *string) func() {
   160  	return func() {
   161  		found := false
   162  		bdlist, err := cli.ListBlockDevices()
   163  		Expect(err).ToNot(HaveOccurred())
   164  		Expect(bdlist).ToNot(BeNil())
   165  		for _, bd := range bdlist.Items {
   166  			if bd.Spec.Path == diskName {
   167  				found = true
   168  				*bdName = bd.Name
   169  				*bdNamespace = bd.Namespace
   170  			}
   171  		}
   172  		Expect(found).To(BeTrue())
   173  	}
   174  }