github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/loggingtrait/loggingtrait_helper.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 loggingtrait
     5  
     6  import (
     7  	"fmt"
     8  	"time"
     9  
    10  	"github.com/onsi/ginkgo/v2"
    11  	"github.com/onsi/gomega"
    12  	"github.com/verrazzano/verrazzano/pkg/k8s/resource"
    13  	"github.com/verrazzano/verrazzano/tests/e2e/pkg"
    14  	"github.com/verrazzano/verrazzano/tests/e2e/pkg/test/framework"
    15  	"github.com/verrazzano/verrazzano/tests/e2e/pkg/test/framework/metrics"
    16  	v1 "k8s.io/api/core/v1"
    17  	"k8s.io/apimachinery/pkg/api/errors"
    18  )
    19  
    20  const (
    21  	shortWaitTimeout     = 10 * time.Minute
    22  	shortPollingInterval = 10 * time.Second
    23  	longWaitTimeout      = 15 * time.Minute
    24  	longPollingInterval  = 20 * time.Second
    25  )
    26  
    27  func DeployApplication(namespace, istionInjection, componentsPath, applicationPath, podName string, t *framework.TestFramework) {
    28  	t.Logs.Info("Deploy test application")
    29  	start := time.Now()
    30  
    31  	t.Logs.Info("Create namespace")
    32  	gomega.Eventually(func() (*v1.Namespace, error) {
    33  		nsLabels := map[string]string{
    34  			"verrazzano-managed": "true",
    35  			"istio-injection":    istionInjection}
    36  		return pkg.CreateNamespace(namespace, nsLabels)
    37  	}, shortWaitTimeout, shortPollingInterval).ShouldNot(gomega.BeNil())
    38  
    39  	t.Logs.Info("Create component resources")
    40  	gomega.Eventually(func() error {
    41  		file, err := pkg.FindTestDataFile(componentsPath)
    42  		if err != nil {
    43  			return err
    44  		}
    45  		return resource.CreateOrUpdateResourceFromFileInGeneratedNamespace(file, namespace)
    46  	}, shortWaitTimeout, shortPollingInterval).ShouldNot(gomega.HaveOccurred())
    47  
    48  	t.Logs.Info("Create application resources")
    49  	gomega.Eventually(func() error {
    50  		file, err := pkg.FindTestDataFile(applicationPath)
    51  		if err != nil {
    52  			return err
    53  		}
    54  		return resource.CreateOrUpdateResourceFromFileInGeneratedNamespace(file, namespace)
    55  	}, shortWaitTimeout, shortPollingInterval).ShouldNot(gomega.HaveOccurred())
    56  
    57  	t.Logs.Infof("Check pod %v is running", podName)
    58  	gomega.Eventually(func() bool {
    59  		result, err := pkg.PodsRunning(namespace, []string{podName})
    60  		if err != nil {
    61  			ginkgo.AbortSuite(fmt.Sprintf("Pod %v is not running in the namespace: %v, error: %v", podName, namespace, err))
    62  		}
    63  		return result
    64  	}, longWaitTimeout, longPollingInterval).Should(gomega.BeTrue())
    65  
    66  	metrics.Emit(t.Metrics.With("deployment_elapsed_time", time.Since(start).Milliseconds()))
    67  }
    68  
    69  func UndeployApplication(namespace string, componentsPath string, applicationPath string, configMapName string, t *framework.TestFramework) {
    70  	t.Logs.Info("Delete application")
    71  	start := time.Now()
    72  	gomega.Eventually(func() error {
    73  		file, err := pkg.FindTestDataFile(applicationPath)
    74  		if err != nil {
    75  			return err
    76  		}
    77  		return resource.DeleteResourceFromFileInGeneratedNamespace(file, namespace)
    78  	}, shortWaitTimeout, shortPollingInterval).ShouldNot(gomega.HaveOccurred())
    79  
    80  	t.Logs.Info("Delete components")
    81  	gomega.Eventually(func() error {
    82  		file, err := pkg.FindTestDataFile(componentsPath)
    83  		if err != nil {
    84  			return err
    85  		}
    86  		return resource.DeleteResourceFromFileInGeneratedNamespace(file, namespace)
    87  	}, shortWaitTimeout, shortPollingInterval).ShouldNot(gomega.HaveOccurred())
    88  
    89  	t.Logs.Info("Verify ConfigMap is Deleted")
    90  	gomega.Eventually(func() bool {
    91  		configMap, _ := pkg.GetConfigMap(configMapName, namespace)
    92  		return (configMap == nil)
    93  	}, shortWaitTimeout, shortPollingInterval).Should(gomega.BeTrue())
    94  
    95  	t.Logs.Info("Delete namespace")
    96  	gomega.Eventually(func() error {
    97  		return pkg.DeleteNamespace(namespace)
    98  	}, shortWaitTimeout, shortPollingInterval).ShouldNot(gomega.HaveOccurred())
    99  
   100  	t.Logs.Info("Wait for Finalizer to be removed")
   101  	gomega.Eventually(func() bool {
   102  		return pkg.CheckNamespaceFinalizerRemoved(namespace)
   103  	}, shortWaitTimeout, shortPollingInterval).Should(gomega.BeTrue())
   104  
   105  	t.Logs.Info("Wait for namespace to be deleted")
   106  	gomega.Eventually(func() bool {
   107  		_, err := pkg.GetNamespace(namespace)
   108  		return err != nil && errors.IsNotFound(err)
   109  	}, shortWaitTimeout, shortPollingInterval).Should(gomega.BeTrue())
   110  	metrics.Emit(t.Metrics.With("undeployment_elapsed_time", time.Since(start).Milliseconds()))
   111  }