github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/integration_tests/sanity/k8s_tests.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/integration_tests/k8s"
    23  	"strings"
    24  )
    25  
    26  const (
    27  	// SparseBlockDeviceName is the name given to blockDevice CRs created for a
    28  	// sparse image
    29  	SparseBlockDeviceName = "sparse-"
    30  	// BlockDeviceName is the name of the blockDevice CRs created corresponding to
    31  	// physical/virtual disks or blockdevices
    32  	BlockDeviceName = "blockdevice-"
    33  	// DiskImageSize is the default file size(1GB) used while creating backing image
    34  	DiskImageSize = 1073741824
    35  	// OperatorPodPrefix is the pod name for NDM operator
    36  	OperatorPodPrefix = "node-disk-operator"
    37  	// DaemonSetPodPrefix is the pod name for NDM daemon
    38  	DaemonSetPodPrefix = "node-disk-manager"
    39  )
    40  
    41  var _ = Describe("NDM Setup Tests", func() {
    42  
    43  	var err error
    44  
    45  	k8sClient, _ := k8s.GetClientSet()
    46  	Context("Checking for Daemonset pods in the cluster", func() {
    47  
    48  		It("should have running ndm pod on each node after installation", func() {
    49  
    50  			By("creating NDM daemonset")
    51  			err = k8sClient.CreateNDMDaemonSet()
    52  			Expect(err).NotTo(HaveOccurred())
    53  
    54  			By("waiting for daemonset pods to be in running state")
    55  			ok := WaitForPodToBeRunningEventually(DaemonSetPodPrefix)
    56  			Expect(ok).To(BeTrue())
    57  		})
    58  
    59  		It("should not have any ndm pods after deletion", func() {
    60  
    61  			By("deleting NDM deamonset")
    62  			err = k8sClient.DeleteNDMDaemonSet()
    63  			Expect(err).NotTo(HaveOccurred())
    64  
    65  			By("no of daemonset pods should be zero")
    66  			ok := WaitForPodToBeDeletedEventually(DaemonSetPodPrefix)
    67  			Expect(ok).To(BeTrue())
    68  		})
    69  	})
    70  })
    71  
    72  // WaitForPodToBeRunningEventually waits for 2 minutes for the given pod to be
    73  // in running state
    74  func WaitForPodToBeRunningEventually(podPrefix string) bool {
    75  	return Eventually(func() string {
    76  		c, err := k8s.GetClientSet()
    77  		Expect(err).NotTo(HaveOccurred())
    78  		pods, err := c.ListPodStatus()
    79  		Expect(err).NotTo(HaveOccurred())
    80  		for pod, state := range pods {
    81  			if strings.Contains(pod, podPrefix) {
    82  				return state
    83  			}
    84  		}
    85  		return ""
    86  	}, 120, 5).Should(Equal(k8s.Running))
    87  }
    88  
    89  // WaitForPodToBeDeletedEventually waits for 2 minutes for the given pod to
    90  // get deleted
    91  func WaitForPodToBeDeletedEventually(podPrefix string) bool {
    92  	return Eventually(func() int {
    93  		c, err := k8s.GetClientSet()
    94  		Expect(err).NotTo(HaveOccurred())
    95  		pods, err := c.ListPodStatus()
    96  		Expect(err).NotTo(HaveOccurred())
    97  
    98  		noOfPods := 0
    99  		for pod := range pods {
   100  			if strings.Contains(pod, podPrefix) {
   101  				noOfPods++
   102  			}
   103  		}
   104  		return noOfPods
   105  	}, 120, 5).Should(BeZero())
   106  }