github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/upgrade/pre-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  	"fmt"
     8  	"time"
     9  
    10  	"github.com/verrazzano/verrazzano/pkg/k8s/resource"
    11  	v1 "k8s.io/api/core/v1"
    12  
    13  	"github.com/onsi/ginkgo/v2"
    14  	"github.com/onsi/gomega"
    15  	"github.com/verrazzano/verrazzano/tests/e2e/pkg"
    16  	"github.com/verrazzano/verrazzano/tests/e2e/pkg/test/framework"
    17  )
    18  
    19  const (
    20  	shortWaitTimeout     = 10 * time.Minute
    21  	shortPollingInterval = 10 * time.Second
    22  	longWaitTimeout      = 15 * time.Minute
    23  	longPollingInterval  = 20 * time.Second
    24  )
    25  
    26  func createNamespace(namespace, istioInjection string, t framework.TestFramework) {
    27  	t.Logs.Info("Create namespace")
    28  	gomega.Eventually(func() (*v1.Namespace, error) {
    29  		nsLabels := map[string]string{"verrazzano-managed": "true", "istio-injeciton": istioInjection}
    30  		nsExists, err := pkg.DoesNamespaceExist(namespace)
    31  		if err != nil {
    32  			t.Logs.Errorf("Could not verify if namespace %s exists", namespace)
    33  			return nil, err
    34  		}
    35  		if !nsExists {
    36  			t.Logs.Infof("Namespace %s does not exist, creating now", namespace)
    37  			nsObject, err := pkg.CreateNamespace(namespace, nsLabels)
    38  			if err != nil {
    39  				t.Logs.Errorf("Failed to create the Namespace %s in the cluster: %v", namespace, err)
    40  			}
    41  			return nsObject, err
    42  		}
    43  		nsObject, err := pkg.GetNamespace(namespace)
    44  		if err != nil {
    45  			t.Logs.Errorf("Failed to get the Namespace %s from the cluster: %v", namespace, err)
    46  		}
    47  		return nsObject, err
    48  	}, shortWaitTimeout, shortPollingInterval).ShouldNot(gomega.BeNil())
    49  }
    50  
    51  // deployApplication deploys an application and namespace given the application parameters
    52  func deployApplication(namespace, yamlPath, podPrefix string, t framework.TestFramework) {
    53  	t.Logs.Info("Create application from yaml path")
    54  	gomega.Eventually(func() error {
    55  		file, err := pkg.FindTestDataFile(yamlPath)
    56  		if err != nil {
    57  			return err
    58  		}
    59  		err = resource.CreateOrUpdateResourceFromFileInGeneratedNamespace(file, namespace)
    60  		if err != nil {
    61  			t.Logs.Errorf("Failed to apply the Application from file: %v", err)
    62  		}
    63  		return err
    64  	}, shortWaitTimeout, shortPollingInterval).ShouldNot(gomega.HaveOccurred())
    65  
    66  	t.Logs.Info("Check application pods are running")
    67  	gomega.Eventually(func() bool {
    68  		result, err := pkg.PodsRunning(namespace, []string{podPrefix})
    69  		if err != nil {
    70  			ginkgo.AbortSuite(fmt.Sprintf("One or more pods are not running in the namespace: %v, error: %v", namespace, err))
    71  		}
    72  		return result
    73  	}, longWaitTimeout, longPollingInterval).Should(gomega.BeTrue())
    74  }
    75  
    76  // deployConfigMap deploys a ConfigMap from a file path
    77  func deployConfigMap(namespace, configMapYamlPath string, t framework.TestFramework) {
    78  	t.Logs.Info("Create ConfigMap resource")
    79  	gomega.Eventually(func() error {
    80  		file, err := pkg.FindTestDataFile(configMapYamlPath)
    81  		if err != nil {
    82  			return err
    83  		}
    84  		err = resource.CreateOrUpdateResourceFromFileInGeneratedNamespace(file, namespace)
    85  		if err != nil {
    86  			t.Logs.Errorf("Failed to apply the ConfigMap from file: %v", err)
    87  		}
    88  		return err
    89  	}, shortWaitTimeout, shortPollingInterval).ShouldNot(gomega.HaveOccurred())
    90  }
    91  
    92  // deployTemplate deploys a Metrics Template from a file path
    93  func deployTemplate(namespace, templateYamlPath string, t framework.TestFramework) {
    94  	t.Logs.Info("Create template resource")
    95  	gomega.Eventually(func() error {
    96  		file, err := pkg.FindTestDataFile(templateYamlPath)
    97  		if err != nil {
    98  			return err
    99  		}
   100  		err = resource.CreateOrUpdateResourceFromFileInGeneratedNamespace(file, namespace)
   101  		if err != nil {
   102  			t.Logs.Errorf("Failed to apply the Metrics Template from file: %v", err)
   103  		}
   104  		return err
   105  	}, shortWaitTimeout, shortPollingInterval).ShouldNot(gomega.HaveOccurred())
   106  }