github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/integration_tests/sanity/ndm_integration_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  	"context"
    21  	"io"
    22  	"testing"
    23  
    24  	. "github.com/onsi/ginkgo"
    25  	. "github.com/onsi/gomega"
    26  	"github.com/openebs/node-disk-manager/integration_tests/k8s"
    27  	"github.com/openebs/node-disk-manager/integration_tests/utils"
    28  	corev1 "k8s.io/api/core/v1"
    29  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    30  	"k8s.io/client-go/kubernetes"
    31  	"k8s.io/client-go/tools/clientcmd"
    32  )
    33  
    34  var k8sGoClient *kubernetes.Clientset
    35  
    36  func TestNDM(t *testing.T) {
    37  	// Setup go client for fetching pod logs on failure
    38  	kubeConfigPath, err := utils.GetConfigPath()
    39  	if err != nil {
    40  		t.Fatal(err)
    41  	}
    42  	config, err := clientcmd.BuildConfigFromFlags("", kubeConfigPath)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	goClient, err := kubernetes.NewForConfig(config)
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  	k8sGoClient = goClient
    51  
    52  	// Custom fail handler to print pod logs on failure
    53  	RegisterFailHandler(NdmFail)
    54  	RunSpecs(t, "Integration Test Suite")
    55  }
    56  
    57  // Initialize the suite
    58  var _ = BeforeSuite(func() {
    59  	// Create the client set
    60  	c, err := k8s.GetClientSet()
    61  	Expect(err).NotTo(HaveOccurred())
    62  
    63  	// Create the crds
    64  	err = c.CreateNDMCRDs()
    65  	Expect(err).NotTo(HaveOccurred())
    66  
    67  	err = c.CreateOpenEBSNamespace()
    68  	Expect(err).NotTo(HaveOccurred())
    69  
    70  	// Create service account and cluster roles required for NDM
    71  	err = c.CreateNDMServiceAccount()
    72  	Expect(err).NotTo(HaveOccurred())
    73  
    74  	err = c.CreateNDMClusterRole()
    75  	Expect(err).NotTo(HaveOccurred())
    76  
    77  	err = c.CreateNDMClusterRoleBinding()
    78  	Expect(err).NotTo(HaveOccurred())
    79  
    80  	err = c.CreateNDMConfigMap()
    81  	Expect(err).NotTo(HaveOccurred())
    82  
    83  	err = c.CreateNDMOperatorDeployment()
    84  	Expect(err).NotTo(HaveOccurred())
    85  
    86  	// wait for all changes to happen
    87  	k8s.WaitForStateChange()
    88  
    89  })
    90  
    91  // clean up all resources by NDM
    92  var _ = AfterSuite(func() {
    93  	c, err := k8s.GetClientSet()
    94  	Expect(err).NotTo(HaveOccurred())
    95  
    96  	err = c.DeleteNDMClusterRoleBinding()
    97  	Expect(err).NotTo(HaveOccurred())
    98  
    99  	err = c.DeleteNDMClusterRole()
   100  	Expect(err).NotTo(HaveOccurred())
   101  
   102  	err = c.DeleteNDMServiceAccount()
   103  	Expect(err).NotTo(HaveOccurred())
   104  
   105  	err = c.DeleteNDMCRDs()
   106  	Expect(err).NotTo(HaveOccurred())
   107  
   108  	err = c.DeleteNDMConfigMap()
   109  	Expect(err).NotTo(HaveOccurred())
   110  
   111  	err = c.DeleteNDMOperatorDeployment()
   112  	Expect(err).NotTo(HaveOccurred())
   113  
   114  	err = c.DeleteOpenEBSNamespace()
   115  	Expect(err).NotTo(HaveOccurred())
   116  })
   117  
   118  func NdmFail(message string, callerSkip ...int) {
   119  	printNDMPodLogs()
   120  	Fail(message, callerSkip...)
   121  }
   122  
   123  func printNDMPodLogs() {
   124  	podInterface := k8sGoClient.CoreV1().Pods(k8s.Namespace)
   125  	podList, err := podInterface.List(context.TODO(),
   126  		metav1.ListOptions{LabelSelector: "name=" + DaemonSetPodPrefix})
   127  	if err != nil {
   128  		GinkgoWriter.Write([]byte("could not fetch pod logs: " + err.Error()))
   129  		return
   130  	}
   131  
   132  	if len(podList.Items) == 0 {
   133  		GinkgoWriter.Write([]byte("could not find ndm pod"))
   134  		return
   135  	}
   136  
   137  	if len(podList.Items) > 1 {
   138  		GinkgoWriter.Write(
   139  			[]byte("found more than one ndm pods. will pick the first pod in the list"))
   140  	}
   141  
   142  	podName := podList.Items[0].Name
   143  	podLogs, err := podInterface.GetLogs(podName,
   144  		&corev1.PodLogOptions{}).Stream(context.TODO())
   145  	if err != nil {
   146  		GinkgoWriter.Write([]byte("could not fetch pod logs: " + err.Error()))
   147  		return
   148  	}
   149  	defer podLogs.Close()
   150  	GinkgoWriter.Write([]byte("------------------ NDM Logs ------------------\n"))
   151  	_, err = io.Copy(GinkgoWriter, podLogs)
   152  	if err != nil {
   153  		GinkgoWriter.Write([]byte("could not write pod logs: " + err.Error()))
   154  		return
   155  	}
   156  	GinkgoWriter.Write([]byte("-------------------- END --------------------\n"))
   157  }