github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/workloads/oam/oam_workload_test.go (about)

     1  // Copyright (c) 2022, 2023, 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 oam
     5  
     6  import (
     7  	dump "github.com/verrazzano/verrazzano/tests/e2e/pkg/test/clusterdump"
     8  	"time"
     9  
    10  	"github.com/verrazzano/verrazzano/pkg/k8s/resource"
    11  
    12  	. "github.com/onsi/ginkgo/v2"
    13  	. "github.com/onsi/gomega"
    14  
    15  	"github.com/verrazzano/verrazzano/tests/e2e/pkg"
    16  	"github.com/verrazzano/verrazzano/tests/e2e/pkg/test/framework"
    17  	"github.com/verrazzano/verrazzano/tests/e2e/pkg/test/framework/metrics"
    18  	v1 "k8s.io/api/core/v1"
    19  	"k8s.io/apimachinery/pkg/api/errors"
    20  )
    21  
    22  const (
    23  	shortWaitTimeout     = 2 * time.Minute
    24  	shortPollingInterval = 10 * time.Second
    25  	longWaitTimeout      = 5 * time.Minute
    26  
    27  	appConfiguration  = "tests/testdata/test-applications/oam/oam-app.yaml"
    28  	compConfiguration = "tests/testdata/test-applications/oam/oam-comp.yaml"
    29  
    30  	pvcName = "test-pvc"
    31  	pvName  = "test-pv"
    32  )
    33  
    34  var (
    35  	t                  = framework.NewTestFramework("oam_workloads")
    36  	generatedNamespace = pkg.GenerateNamespace("oam-workloads")
    37  )
    38  
    39  var beforeSuite = t.BeforeSuiteFunc(func() {
    40  	if !skipDeploy {
    41  		start := time.Now()
    42  		deployOAMApp()
    43  		metrics.Emit(t.Metrics.With("deployment_elapsed_time", time.Since(start).Milliseconds()))
    44  	}
    45  	beforeSuitePassed = true
    46  })
    47  
    48  var _ = BeforeSuite(beforeSuite)
    49  
    50  var failed = false
    51  var beforeSuitePassed = false
    52  var _ = t.AfterEach(func() {
    53  	failed = failed || CurrentSpecReport().Failed()
    54  })
    55  
    56  var afterSuite = t.AfterSuiteFunc(func() {
    57  	if failed || !beforeSuitePassed {
    58  		dump.ExecuteBugReport(namespace)
    59  	}
    60  	if !skipUndeploy {
    61  		undeployOAMApp()
    62  	}
    63  })
    64  
    65  var _ = AfterSuite(afterSuite)
    66  
    67  var _ = t.Describe("An OAM application is deployed", Label("f:app-lcm.oam"), func() {
    68  	t.Context("Check for created resources", func() {
    69  		// GIVEN the test application is deployed
    70  		// AND the application includes a component that wraps a persistent volume claim
    71  		// WHEN a call is made to fetch the persistent volume claim
    72  		// THEN the persistent volume claim is found to exist
    73  		t.It("The persistent volume claim exists", func() {
    74  			Eventually(func() (bool, error) {
    75  				return volumeClaimExists(pvcName)
    76  			}, shortWaitTimeout, shortPollingInterval).Should(BeTrue())
    77  		})
    78  
    79  		t.It("The persistent volume exists", func() {
    80  			Eventually(func() (bool, error) {
    81  				return pvExists(pvName)
    82  			}, shortWaitTimeout, shortPollingInterval).Should(BeTrue())
    83  		})
    84  	})
    85  })
    86  
    87  func pvExists(name string) (bool, error) {
    88  	volumes, err := pkg.GetPersistentVolumes()
    89  	if err != nil {
    90  		return false, err
    91  	}
    92  	_, ok := volumes[name]
    93  	return ok, nil
    94  }
    95  
    96  // volumeClaimExists checks if the persistent volume claim with the specified name exists
    97  // in the test namespace
    98  func volumeClaimExists(volumeClaimName string) (bool, error) {
    99  	volumeClaims, err := pkg.GetPersistentVolumeClaims(namespace)
   100  	if err != nil {
   101  		return false, err
   102  	}
   103  	_, ok := volumeClaims[volumeClaimName]
   104  	return ok, nil
   105  }
   106  
   107  // deployOAMApp deploys the test components and application configuration
   108  func deployOAMApp() {
   109  	t.Logs.Info("Deploy OAM application")
   110  
   111  	t.Logs.Info("Create namespace")
   112  	Eventually(func() (*v1.Namespace, error) {
   113  		nsLabels := map[string]string{
   114  			"verrazzano-managed": "true"}
   115  		return pkg.CreateNamespace(namespace, nsLabels)
   116  	}, shortWaitTimeout, shortPollingInterval).ShouldNot(BeNil())
   117  
   118  	t.Logs.Info("Create component resources")
   119  	Eventually(func() error {
   120  		file, err := pkg.FindTestDataFile(compConfiguration)
   121  		if err != nil {
   122  			return err
   123  		}
   124  		return resource.CreateOrUpdateResourceFromFileInGeneratedNamespace(file, namespace)
   125  	}, shortWaitTimeout, shortPollingInterval, "Failed to create component resources for Coherence application").ShouldNot(HaveOccurred())
   126  
   127  	t.Logs.Info("Create application resources")
   128  	Eventually(func() error {
   129  		file, err := pkg.FindTestDataFile(appConfiguration)
   130  		if err != nil {
   131  			return err
   132  		}
   133  		return resource.CreateOrUpdateResourceFromFileInGeneratedNamespace(file, namespace)
   134  	}, shortWaitTimeout, shortPollingInterval).ShouldNot(HaveOccurred())
   135  }
   136  
   137  // undeployOAMApp removes the test components and application configuration
   138  func undeployOAMApp() {
   139  	t.Logs.Info("Undeploy OAM application")
   140  	start := time.Now()
   141  	Eventually(func() error {
   142  		file, err := pkg.FindTestDataFile(appConfiguration)
   143  		if err != nil {
   144  			return err
   145  		}
   146  		return resource.DeleteResourceFromFileInGeneratedNamespace(file, namespace)
   147  	}, shortWaitTimeout, shortPollingInterval).ShouldNot(HaveOccurred())
   148  
   149  	t.Logs.Info("Delete components")
   150  	Eventually(func() error {
   151  		file, err := pkg.FindTestDataFile(compConfiguration)
   152  		if err != nil {
   153  			return err
   154  		}
   155  		return resource.DeleteResourceFromFileInGeneratedNamespace(file, namespace)
   156  	}, shortWaitTimeout, shortPollingInterval).ShouldNot(HaveOccurred())
   157  
   158  	t.Logs.Info("Wait for persistent volume claim to terminate")
   159  	Eventually(func() (bool, error) {
   160  		return volumeClaimExists(pvcName)
   161  	}, shortWaitTimeout, shortPollingInterval).Should(BeFalse())
   162  
   163  	t.Logs.Info("Delete namespace")
   164  	Eventually(func() error {
   165  		return pkg.DeleteNamespace(namespace)
   166  	}, shortWaitTimeout, shortPollingInterval).ShouldNot(HaveOccurred())
   167  
   168  	t.Logs.Info("Wait for namespace finalizer to be removed")
   169  	Eventually(func() bool {
   170  		return pkg.CheckNamespaceFinalizerRemoved(namespace)
   171  	}, shortWaitTimeout, shortPollingInterval).Should(BeTrue())
   172  
   173  	t.Logs.Info("Wait for namespace deletion")
   174  	Eventually(func() bool {
   175  		_, err := pkg.GetNamespace(namespace)
   176  		return err != nil && errors.IsNotFound(err)
   177  	}, longWaitTimeout, shortPollingInterval).Should(BeTrue())
   178  
   179  	metrics.Emit(t.Metrics.With("undeployment_elapsed_time", time.Since(start).Milliseconds()))
   180  }