github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/uninstall/verifyrancher/verify_rancher_crs_test.go (about)

     1  // Copyright (c) 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package verifyrancher
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	. "github.com/onsi/ginkgo/v2"
    10  	. "github.com/onsi/gomega"
    11  	"github.com/verrazzano/verrazzano/pkg/k8sutil"
    12  	"github.com/verrazzano/verrazzano/tests/e2e/pkg"
    13  	"github.com/verrazzano/verrazzano/tests/e2e/pkg/test/framework"
    14  	apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    15  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    16  	"k8s.io/apimachinery/pkg/runtime/schema"
    17  	"k8s.io/client-go/dynamic"
    18  	"strings"
    19  	"time"
    20  )
    21  
    22  const (
    23  	waitTimeout     = 2 * time.Minute
    24  	pollingInterval = 5 * time.Second
    25  )
    26  
    27  var (
    28  	crds      *apiextv1.CustomResourceDefinitionList
    29  	clientset dynamic.Interface
    30  )
    31  
    32  var t = framework.NewTestFramework("uninstall verify Rancher CRs")
    33  
    34  // This test verifies that Rancher custom resources have been deleted from the cluster.
    35  var _ = t.Describe("Verify Rancher custom resources", Label("f:platform-lcm.unnstall"), func() {
    36  	t.It("Check for unexpected Rancher custom resources", func() {
    37  		Eventually(func() (*apiextv1.CustomResourceDefinitionList, error) {
    38  			var err error
    39  			crds, err = pkg.ListCRDs()
    40  			return crds, err
    41  		}, waitTimeout, pollingInterval).ShouldNot(BeNil())
    42  
    43  		Eventually(func() (dynamic.Interface, error) {
    44  			kubePath, err := k8sutil.GetKubeConfigLocation()
    45  			if err != nil {
    46  				return nil, err
    47  			}
    48  			clientset, err = pkg.GetDynamicClientInCluster(kubePath)
    49  			return clientset, err
    50  		}, waitTimeout, pollingInterval).ShouldNot(BeNil())
    51  
    52  		unexpectedCRs := false
    53  
    54  		for _, crd := range crds.Items {
    55  			if strings.HasSuffix(crd.Name, ".cattle.io") {
    56  				for _, version := range crd.Spec.Versions {
    57  					rancherCRs, err := clientset.Resource(schema.GroupVersionResource{
    58  						Group:    crd.Spec.Group,
    59  						Version:  version.Name,
    60  						Resource: crd.Spec.Names.Plural,
    61  					}).List(context.TODO(), metav1.ListOptions{})
    62  					if err != nil {
    63  						Expect(err).To(Not(HaveOccurred()), fmt.Sprintf("Failed listing custom resources for %s", crd.Spec.Names.Kind))
    64  					}
    65  					if len(rancherCRs.Items) == 0 {
    66  						continue
    67  					}
    68  					for _, rancherCR := range rancherCRs.Items {
    69  						pkg.Log(pkg.Error, fmt.Sprintf("Unexpected custom resource %s/%s was found for %s.%s/%s", rancherCR.GetNamespace(), rancherCR.GetName(), crd.Spec.Names.Singular, crd.Spec.Group, version.Name))
    70  						unexpectedCRs = true
    71  					}
    72  				}
    73  			}
    74  		}
    75  
    76  		if unexpectedCRs {
    77  			Fail("Failed to verify Rancher custom resources")
    78  		}
    79  	})
    80  })