github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/logging/opensearch/opensearch_retention_policy_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 opensearch
     5  
     6  import (
     7  	"fmt"
     8  	. "github.com/onsi/ginkgo/v2"
     9  	. "github.com/onsi/gomega"
    10  	"github.com/verrazzano/verrazzano/pkg/k8sutil"
    11  	"github.com/verrazzano/verrazzano/tests/e2e/pkg"
    12  	"time"
    13  )
    14  
    15  var _ = t.Describe("Opensearch Retention Policies Suite", Label("f:observability.logging.es"), func() {
    16  	// It Wrapper to only run spec if component is supported on the current Verrazzano installation
    17  	MinimumVerrazzanoIt := func(description string, f func()) {
    18  		kubeconfigPath, err := k8sutil.GetKubeConfigLocation()
    19  		if err != nil {
    20  			t.It(description, func() {
    21  				Fail(fmt.Sprintf("Failed to get default kubeconfig path: %s", err.Error()))
    22  			})
    23  		}
    24  		supported, err := pkg.IsVerrazzanoMinVersion("1.3.0", kubeconfigPath)
    25  		if err != nil {
    26  			t.It(description, func() {
    27  				Fail(err.Error())
    28  			})
    29  		}
    30  		// Only run tests if Verrazzano is at least version 1.3.0
    31  		if supported {
    32  			t.It(description, f)
    33  		} else {
    34  			pkg.Log(pkg.Info, fmt.Sprintf("Skipping check '%v', Verrazzano is not at version 1.3.0", description))
    35  		}
    36  	}
    37  
    38  	MinimumVerrazzanoIt("System log Retention policy in ISM should match configuration value in VZ CR", func() {
    39  		Eventually(func() bool {
    40  			systemRetentionPolicy, err := pkg.GetVerrazzanoRetentionPolicy(pkg.SystemLogIsmPolicyName)
    41  			if err != nil {
    42  				pkg.Log(pkg.Error, err.Error())
    43  				return false
    44  			}
    45  			policyExists, err := pkg.ISMPolicyExists(systemRetentionPolicy.PolicyName)
    46  			if err != nil {
    47  				pkg.Log(pkg.Error, err.Error())
    48  				return false
    49  			}
    50  			minIndexAge, err := pkg.GetRetentionPeriod(systemRetentionPolicy.PolicyName)
    51  			if err != nil {
    52  				pkg.Log(pkg.Error, err.Error())
    53  				return false
    54  			}
    55  			if systemRetentionPolicy.MinIndexAge == nil {
    56  				pkg.Log(pkg.Error, "MinIndexAge for system ISM policy in VZ CR is nil")
    57  				return false
    58  			}
    59  			return policyExists && minIndexAge == *systemRetentionPolicy.MinIndexAge
    60  		}).WithPolling(shortPollingInterval).WithTimeout(longWaitTimeout).Should(BeTrue(), "ISM policy for system indices should be created")
    61  	})
    62  
    63  	MinimumVerrazzanoIt("Application log Retention policy in ISM should match configuration value in VZ CR", func() {
    64  		Eventually(func() bool {
    65  			applicationRetentionPolicy, err := pkg.GetVerrazzanoRetentionPolicy(pkg.ApplicationLogIsmPolicyName)
    66  			if err != nil {
    67  				pkg.Log(pkg.Error, err.Error())
    68  				return false
    69  			}
    70  			policyExists, err := pkg.ISMPolicyExists(applicationRetentionPolicy.PolicyName)
    71  			if err != nil {
    72  				pkg.Log(pkg.Error, err.Error())
    73  				return false
    74  			}
    75  			minIndexAge, err := pkg.GetRetentionPeriod(applicationRetentionPolicy.PolicyName)
    76  			if err != nil {
    77  				pkg.Log(pkg.Error, err.Error())
    78  				return false
    79  			}
    80  			if applicationRetentionPolicy.MinIndexAge == nil {
    81  				pkg.Log(pkg.Error, "MinIndexAge for application ISM policy in VZ CR is nil")
    82  				return false
    83  			}
    84  			return policyExists && minIndexAge == *applicationRetentionPolicy.MinIndexAge
    85  		}).WithPolling(shortPollingInterval).WithTimeout(longWaitTimeout).Should(BeTrue(), "ISM policy for application indices should be created")
    86  	})
    87  
    88  	MinimumVerrazzanoIt("Check no system indices exists older than the retention period specified", func() {
    89  		currentEpochTime := time.Now().Unix()
    90  		systemRetentionPolicy, err := pkg.GetVerrazzanoRetentionPolicy(pkg.SystemLogIsmPolicyName)
    91  		if err != nil {
    92  			pkg.Log(pkg.Error, err.Error())
    93  			Fail(err.Error())
    94  		}
    95  		retentionPeriod, err := pkg.CalculateSeconds(*systemRetentionPolicy.MinIndexAge)
    96  		if err != nil {
    97  			pkg.Log(pkg.Error, err.Error())
    98  			Fail(err.Error())
    99  		}
   100  		// Buffer time added to allow ISM policy time to clean up.
   101  		// Link to documentation.
   102  		oldestAllowedTimestamp := (currentEpochTime - retentionPeriod) * 1000
   103  		indexMetadataList, err := pkg.GetBackingIndicesForDataStream(pkg.SystemLogIsmPolicyName)
   104  		if err != nil {
   105  			pkg.Log(pkg.Error, err.Error())
   106  			Fail(err.Error())
   107  		}
   108  		olderIndexFound, err := pkg.ContainsIndicesOlderThanRetentionPeriod(indexMetadataList, oldestAllowedTimestamp)
   109  		if err != nil {
   110  			pkg.Log(pkg.Error, err.Error())
   111  			Fail(err.Error())
   112  		}
   113  		Expect(olderIndexFound).To(Equal(false))
   114  	})
   115  
   116  	MinimumVerrazzanoIt("Check no application indices exists older than the retention period specified", func() {
   117  		currentEpochTime := time.Now().Unix()
   118  		applicationRetentionPolicy, err := pkg.GetVerrazzanoRetentionPolicy(pkg.ApplicationLogIsmPolicyName)
   119  		if err != nil {
   120  			pkg.Log(pkg.Error, err.Error())
   121  			Fail(err.Error())
   122  		}
   123  		retentionPeriod, err := pkg.CalculateSeconds(*applicationRetentionPolicy.MinIndexAge)
   124  		if err != nil {
   125  			pkg.Log(pkg.Error, err.Error())
   126  			Fail(err.Error())
   127  		}
   128  		oldestAllowedTimestamp := (currentEpochTime - retentionPeriod) * 1000
   129  		applicationDataStreams, err := pkg.GetApplicationDataStreamNames()
   130  		if err != nil {
   131  			pkg.Log(pkg.Error, err.Error())
   132  			Fail(err.Error())
   133  		}
   134  		var indexMetadataList []pkg.IndexMetadata
   135  		for _, applicationDataStream := range applicationDataStreams {
   136  			indicesPerDataStream, err := pkg.GetBackingIndicesForDataStream(applicationDataStream)
   137  			if err != nil {
   138  				pkg.Log(pkg.Error, err.Error())
   139  				Fail(err.Error())
   140  			}
   141  			indexMetadataList = append(indexMetadataList, indicesPerDataStream...)
   142  		}
   143  		oldIndexFound, err := pkg.ContainsIndicesOlderThanRetentionPeriod(indexMetadataList, oldestAllowedTimestamp)
   144  		if err != nil {
   145  			pkg.Log(pkg.Error, err.Error())
   146  			Fail(err.Error())
   147  		}
   148  		Expect(oldIndexFound).To(Equal(false))
   149  
   150  	})
   151  
   152  })