github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/logging/opensearch/opensearch_rollover_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  	"sort"
    13  	"strconv"
    14  	"time"
    15  )
    16  
    17  const (
    18  	BufferTime = 10 * 60 // 10 minutes
    19  )
    20  
    21  var _ = t.Describe("Opensearch Rollover Policies Suite", Label("f:observability.logging.es"), func() {
    22  	// It Wrapper to only run spec if component is supported on the current Verrazzano installation
    23  	MinimumVerrazzanoIt := func(description string, f func()) {
    24  		kubeconfigPath, err := k8sutil.GetKubeConfigLocation()
    25  		if err != nil {
    26  			t.It(description, func() {
    27  				Fail(fmt.Sprintf("Failed to get default kubeconfig path: %s", err.Error()))
    28  			})
    29  		}
    30  		supported, err := pkg.IsVerrazzanoMinVersion("1.3.0", kubeconfigPath)
    31  		if err != nil {
    32  			t.It(description, func() {
    33  				Fail(err.Error())
    34  			})
    35  		}
    36  		// Only run tests if Verrazzano is at least version 1.3.0
    37  		if supported {
    38  			t.It(description, f)
    39  		} else {
    40  			pkg.Log(pkg.Info, fmt.Sprintf("Skipping check '%v', Verrazzano is not at version 1.3.0", description))
    41  		}
    42  	}
    43  
    44  	MinimumVerrazzanoIt("System log Rollover policy in ISM should match configuration value in VZ CR", func() {
    45  		Eventually(func() bool {
    46  			rollOverISMPolicy, err := pkg.GetVerrazzanoRolloverPolicy(pkg.SystemLogIsmPolicyName)
    47  			if err != nil {
    48  				pkg.Log(pkg.Error, err.Error())
    49  				return false
    50  			}
    51  			rolloverPeriod, err := pkg.GetISMRolloverPeriod(pkg.SystemLogIsmPolicyName)
    52  			if err != nil {
    53  				pkg.Log(pkg.Error, err.Error())
    54  				return false
    55  			}
    56  			return rolloverPeriod == *rollOverISMPolicy.MinIndexAge
    57  		}).WithPolling(shortPollingInterval).WithTimeout(longWaitTimeout).Should(BeTrue(), "ISM rollover policy for system logs should match user configured value in VZ")
    58  	})
    59  
    60  	MinimumVerrazzanoIt("Application log Rollover policy in ISM should match configuration value in VZ CR", func() {
    61  		Eventually(func() bool {
    62  			rollOverISMPolicy, err := pkg.GetVerrazzanoRolloverPolicy(pkg.ApplicationLogIsmPolicyName)
    63  			if err != nil {
    64  				pkg.Log(pkg.Error, err.Error())
    65  				return false
    66  			}
    67  			rolloverPeriod, err := pkg.GetISMRolloverPeriod(pkg.ApplicationLogIsmPolicyName)
    68  			if err != nil {
    69  				pkg.Log(pkg.Error, err.Error())
    70  				return false
    71  			}
    72  			return rolloverPeriod == *rollOverISMPolicy.MinIndexAge
    73  		}).WithPolling(shortPollingInterval).WithTimeout(longWaitTimeout).Should(BeTrue(), "ISM rollover policy for application logs should match user configured value in VZ")
    74  	})
    75  
    76  	MinimumVerrazzanoIt("Data Stream for system logs if older than rollover period should be having more than 1 indices (one per rollover period)", func() {
    77  		rolloverPeriod, err := pkg.GetISMRolloverPeriod(pkg.SystemLogIsmPolicyName)
    78  		if err != nil {
    79  			Fail(err.Error())
    80  		}
    81  		rolloverPeriodInSeconds, err := pkg.CalculateSeconds(rolloverPeriod)
    82  		if err != nil {
    83  			pkg.Log(pkg.Error, err.Error())
    84  			Fail(err.Error())
    85  		}
    86  		indexMetadataList, err := pkg.GetIndexMetadataForDataStream(pkg.SystemLogIsmPolicyName)
    87  		if err != nil {
    88  			pkg.Log(pkg.Error, err.Error())
    89  			Fail(err.Error())
    90  		}
    91  		pkg.Log(pkg.Info, fmt.Sprintf("Data stream %s contains %d backend indices", pkg.SystemLogIsmPolicyName, len(indexMetadataList)))
    92  
    93  		if len(indexMetadataList) >= 1 {
    94  			currentTime := time.Now().Unix()
    95  			var sortedCreationTimes []int
    96  			for i := 0; i < len(indexMetadataList); i++ {
    97  				creationTime, _ := strconv.Atoi(indexMetadataList[i].CreationDate)
    98  				pkg.Log(pkg.Info, fmt.Sprintf("Creation time of index %s is %d", indexMetadataList[i].ProvidedName, creationTime))
    99  				sortedCreationTimes = append(sortedCreationTimes, creationTime)
   100  			}
   101  			sort.Ints(sortedCreationTimes)
   102  			for i := 0; i < len(sortedCreationTimes)-1; i++ {
   103  				timeDiffInSeconds := int64((sortedCreationTimes[i+1] - sortedCreationTimes[i]) / 1000)
   104  				isCreateTimesInRange := (timeDiffInSeconds > rolloverPeriodInSeconds) &&
   105  					(timeDiffInSeconds < rolloverPeriodInSeconds+BufferTime)
   106  				Expect(isCreateTimesInRange).To(Equal(true))
   107  			}
   108  			// Check if the last index has not been rolled over and it is less than the rollover period
   109  			currentTimeDiffInSeconds := int64((sortedCreationTimes[len(sortedCreationTimes)-1] / 1000)) - currentTime
   110  			Expect(currentTimeDiffInSeconds < rolloverPeriodInSeconds).To(Equal(true))
   111  		} else {
   112  			Fail("Data stream for system logs should have atleast one backing index")
   113  		}
   114  	})
   115  
   116  	MinimumVerrazzanoIt("Data Streams of application logs which are older than rollover period should be having more than one backend indices (one per rollover period)", func() {
   117  		rolloverPeriod, err := pkg.GetISMRolloverPeriod(pkg.ApplicationLogIsmPolicyName)
   118  		if err != nil {
   119  			Fail(err.Error())
   120  		}
   121  		rolloverPeriodInSeconds, err := pkg.CalculateSeconds(rolloverPeriod)
   122  		if err != nil {
   123  			Fail(err.Error())
   124  		}
   125  		applicationDataStreams, err := pkg.GetApplicationDataStreamNames()
   126  		if err != nil {
   127  			Fail(err.Error())
   128  		}
   129  		for _, applicationDataStream := range applicationDataStreams {
   130  			indexMetadataList, err := pkg.GetIndexMetadataForDataStream(applicationDataStream)
   131  			if err != nil {
   132  				Fail("Error getting index metadata for application datastream - " + applicationDataStream + ": " + err.Error())
   133  			}
   134  			pkg.Log(pkg.Info, fmt.Sprintf("Data stream %s contains %d backend indices", pkg.SystemLogIsmPolicyName, len(indexMetadataList)))
   135  			if len(indexMetadataList) >= 1 {
   136  				currentTime := time.Now().Unix()
   137  				var sortedCreationTimes []int
   138  				for i := 0; i < len(indexMetadataList); i++ {
   139  					creationTime, _ := strconv.Atoi(indexMetadataList[i].CreationDate)
   140  					pkg.Log(pkg.Info, fmt.Sprintf("Creation time of index %s is %d", indexMetadataList[i].ProvidedName, creationTime))
   141  					sortedCreationTimes = append(sortedCreationTimes, creationTime)
   142  				}
   143  				sort.Ints(sortedCreationTimes)
   144  				for i := 0; i < len(sortedCreationTimes)-1; i++ {
   145  					timeDiffInSeconds := int64((sortedCreationTimes[i+1] - sortedCreationTimes[i]) / 1000)
   146  					isCreateTimesInRange := (timeDiffInSeconds > rolloverPeriodInSeconds) &&
   147  						(timeDiffInSeconds < rolloverPeriodInSeconds+BufferTime)
   148  					Expect(isCreateTimesInRange).To(Equal(true))
   149  				}
   150  				// Check if the last index has not been rolled over and it is less than the rollover period
   151  				currentTimeDiffInSeconds := int64((sortedCreationTimes[len(sortedCreationTimes)-1] / 1000)) - currentTime
   152  				Expect(currentTimeDiffInSeconds < rolloverPeriodInSeconds).To(Equal(true))
   153  			} else {
   154  				Fail("No index present for data stream - " + applicationDataStream)
   155  			}
   156  		}
   157  	})
   158  
   159  })