github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/upgrade/post-upgrade/metricsbinding/metrics_binding_utils_test.go (about)

     1  // Copyright (c) 2021, 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 metricsbinding
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"time"
    10  
    11  	"github.com/verrazzano/verrazzano/pkg/k8s/resource"
    12  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    13  
    14  	. "github.com/onsi/ginkgo/v2"
    15  	. "github.com/onsi/gomega"
    16  	"github.com/verrazzano/verrazzano/tests/e2e/pkg"
    17  	"github.com/verrazzano/verrazzano/tests/e2e/pkg/test/framework"
    18  	"k8s.io/apimachinery/pkg/api/errors"
    19  )
    20  
    21  const (
    22  	shortWaitTimeout     = 10 * time.Minute
    23  	shortPollingInterval = 10 * time.Second
    24  )
    25  
    26  // undeployApplication removes the application and namespace from the cluster
    27  func undeployApplication(namespace string, yamlPath string, t framework.TestFramework) {
    28  	t.Logs.Infof("Verifying the namespace exists before attempting to delete resources")
    29  	nsExists, err := pkg.DoesNamespaceExist(namespace)
    30  	if err != nil {
    31  		Fail(fmt.Sprintf("Failed to verify if the namespace %s exists", namespace))
    32  	}
    33  	if !nsExists {
    34  		return
    35  	}
    36  	t.Logs.Info("Delete application")
    37  	Eventually(func() error {
    38  		file, err := pkg.FindTestDataFile(yamlPath)
    39  		if err != nil {
    40  			return err
    41  		}
    42  		err = resource.DeleteResourceFromFileInGeneratedNamespace(file, namespace)
    43  		if err != nil {
    44  			t.Logs.Errorf("Failed to delete the Application from file: %v", err)
    45  		}
    46  		return err
    47  	}, shortWaitTimeout, shortPollingInterval).ShouldNot(HaveOccurred())
    48  
    49  	t.Logs.Info("Delete namespace")
    50  	Eventually(func() error {
    51  		err := pkg.DeleteNamespace(namespace)
    52  		if err != nil {
    53  			t.Logs.Errorf("Failed to delete the Namespace %s from the cluster: %v", namespace, err)
    54  		}
    55  		return err
    56  	}, shortWaitTimeout, shortPollingInterval).ShouldNot(HaveOccurred())
    57  
    58  	t.Logs.Info("Wait for namespace finalizer to be removed")
    59  	Eventually(func() bool {
    60  		return pkg.CheckNamespaceFinalizerRemoved(namespace)
    61  	}, shortWaitTimeout, shortPollingInterval).Should(BeTrue())
    62  
    63  	t.Logs.Info("Wait for namespace to be deleted")
    64  	Eventually(func() bool {
    65  		_, err := pkg.GetNamespace(namespace)
    66  		return err != nil && errors.IsNotFound(err)
    67  	}, shortWaitTimeout, shortPollingInterval).Should(BeTrue())
    68  }
    69  
    70  // verifyMetricsBindingsDeleted verifies that a metrics binding does not exist for a given namespace
    71  func verifyMetricsBindingsDeleted(namespace string, t framework.TestFramework) {
    72  	Eventually(func() (bool, error) {
    73  		t.Logs.Infof("Verify no Metrics Bindings exist in the namespace %s", namespace)
    74  		nsExists, err := pkg.DoesNamespaceExist(namespace)
    75  		if err != nil {
    76  			t.Logs.Errorf("Could not verify namespace %s exists", namespace)
    77  			return false, err
    78  		}
    79  		if !nsExists {
    80  			t.Logs.Infof("Namespace %s does not exist, no Metrics Binding check is occurring", namespace)
    81  			return true, nil
    82  		}
    83  		clientset, err := pkg.GetVerrazzanoApplicationOperatorClientSet()
    84  		if err != nil {
    85  			t.Logs.Error("Could not get the Verrazzano Application Operator clientset")
    86  			return false, err
    87  		}
    88  		bindingList, err := clientset.AppV1alpha1().MetricsBindings(namespace).List(context.TODO(), metav1.ListOptions{})
    89  		if err != nil {
    90  			t.Logs.Error("Could list Metrics Bindings in the namespace %s", namespace)
    91  			return false, err
    92  		}
    93  		return len(bindingList.Items) == 0, nil
    94  	}, shortWaitTimeout, shortPollingInterval).Should(BeTrue())
    95  }