github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/upgrade/post-upgrade/opensearch/opensearch_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  	"encoding/json"
     8  	"fmt"
     9  	"github.com/verrazzano/verrazzano/pkg/k8sutil"
    10  	"os"
    11  	"time"
    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  	waitTimeout     = 3 * time.Minute
    21  	pollingInterval = 10 * time.Second
    22  	documentFile    = "testdata/upgrade/opensearch/document1.json"
    23  	longTimeout     = 10 * time.Minute
    24  )
    25  
    26  var t = framework.NewTestFramework("opensearch")
    27  
    28  var _ = t.Describe("Post upgrade OpenSearch", Label("f:observability.logging.es"), func() {
    29  	// It Wrapper to only run spec if component is supported on the current Verrazzano installation
    30  	MinimumVerrazzanoIt := func(description string, f func()) {
    31  		kubeconfigPath, err := k8sutil.GetKubeConfigLocation()
    32  		if err != nil {
    33  			t.It(description, func() {
    34  				Fail(fmt.Sprintf("Failed to get default kubeconfig path: %s", err.Error()))
    35  			})
    36  		}
    37  		supported, err := pkg.IsVerrazzanoMinVersion("1.3.0", kubeconfigPath)
    38  		if err != nil {
    39  			t.It(description, func() {
    40  				Fail(err.Error())
    41  			})
    42  		}
    43  		// Only run tests if Verrazzano is at least version 1.3.0
    44  		if supported {
    45  			t.It(description, f)
    46  		} else {
    47  			pkg.Log(pkg.Info, fmt.Sprintf("Skipping check '%v', Verrazzano is not at version 1.3.0", description))
    48  		}
    49  	}
    50  	// Custom Code for checking ISM Policy...
    51  	MinimumVerrazzanoIt("If ISM already exists, then also default policy created.", func() {
    52  		Eventually(func() bool {
    53  			kubeconfigPath, _ := k8sutil.GetKubeConfigLocation()
    54  			isOSEnabled, err := pkg.IsOpenSearchEnabled(kubeconfigPath)
    55  			if err != nil {
    56  				pkg.Log(pkg.Error, err.Error())
    57  				return false
    58  			}
    59  			if isOSEnabled {
    60  				ismStatus, err := pkg.CheckISMPolicy()
    61  				if err != nil {
    62  					pkg.Log(pkg.Error, err.Error())
    63  					return false
    64  				}
    65  				return ismStatus
    66  
    67  			}
    68  			return true
    69  		}).WithPolling(pollingInterval).WithTimeout(waitTimeout).Should(BeTrue(), "Expected not to find any old indices")
    70  	})
    71  	// GIVEN the OpenSearch pod
    72  	// WHEN the indices are retrieved
    73  	// THEN verify that they do not have the old indices
    74  	MinimumVerrazzanoIt("Old indices are deleted", func() {
    75  		Eventually(func() bool {
    76  			kubeconfigPath, _ := k8sutil.GetKubeConfigLocation()
    77  			isOSEnabled, err := pkg.IsOpenSearchEnabled(kubeconfigPath)
    78  			if err != nil {
    79  				pkg.Log(pkg.Error, err.Error())
    80  				return false
    81  			}
    82  			if isOSEnabled {
    83  				oldIndicesPatterns := []string{"^verrazzano-namespace-.*$", "^verrazzano-systemd-journal$",
    84  					"^verrazzano-logstash-.*$"}
    85  				return pkg.IndicesNotExists(oldIndicesPatterns)
    86  			}
    87  			return true
    88  		}).WithPolling(pollingInterval).WithTimeout(waitTimeout).Should(BeTrue(), "Expected not to find any old indices")
    89  	})
    90  
    91  	// GIVEN the OpenSearch pod
    92  	// WHEN the data streams are retrieved
    93  	// THEN verify that they have data streams
    94  	MinimumVerrazzanoIt("Data streams are created", func() {
    95  		Eventually(func() bool {
    96  			kubeconfigPath, _ := k8sutil.GetKubeConfigLocation()
    97  			isOSEnabled, err := pkg.IsOpenSearchEnabled(kubeconfigPath)
    98  			if err != nil {
    99  				pkg.Log(pkg.Error, err.Error())
   100  				return false
   101  			}
   102  			if isOSEnabled {
   103  				return pkg.CheckForDataStream(pkg.VerrazzanoNamespace)
   104  			}
   105  			return true
   106  		}).WithPolling(pollingInterval).WithTimeout(waitTimeout).Should(BeTrue(), "Expected not to find any old indices")
   107  	})
   108  
   109  	// GIVEN the OpenSearch pod
   110  	// THEN verify that the data can be retrieved successfully
   111  	MinimumVerrazzanoIt("OpenSearch get old data", func() {
   112  		Eventually(func() bool {
   113  			kubeConfigPath, _ := k8sutil.GetKubeConfigLocation()
   114  			isOSEnabled, err := pkg.IsOpenSearchEnabled(kubeConfigPath)
   115  			if err != nil {
   116  				pkg.Log(pkg.Error, err.Error())
   117  				return false
   118  			}
   119  			if isOSEnabled {
   120  				indexName, err := pkg.GetOpenSearchSystemIndex(pkg.VerrazzanoNamespace)
   121  				if err != nil {
   122  					pkg.Log(pkg.Error, fmt.Sprintf("error getting the system index: %v", err))
   123  					return false
   124  				}
   125  				file, err := pkg.FindTestDataFile(documentFile)
   126  				if err != nil {
   127  					pkg.Log(pkg.Error, fmt.Sprintf("failed to find test data file: %v", err))
   128  					return false
   129  				}
   130  				data, err := os.ReadFile(file)
   131  				if err != nil {
   132  					pkg.Log(pkg.Error, fmt.Sprintf("failed to read test data file: %v", err))
   133  					return false
   134  				}
   135  				var dataMap map[string]interface{}
   136  				if err := json.Unmarshal(data, &dataMap); err != nil {
   137  					pkg.Log(pkg.Error, fmt.Sprintf("OpenSearch: Error unmarshalling test document: %v", err))
   138  				}
   139  				query := pkg.OpensearchQuery{
   140  					Filters: []pkg.Match{
   141  						{Key: "type", Value: dataMap["type"].(string)}},
   142  					MustNot: []pkg.Match{},
   143  				}
   144  				result := pkg.SearchLog(fmt.Sprintf("%s/_doc", indexName), query)
   145  				for k, v := range dataMap {
   146  					if result[k] != v {
   147  						pkg.Log(pkg.Error, fmt.Sprintf("Expected to have a document with field name %s and value %s", k, v))
   148  						return false
   149  					}
   150  				}
   151  			}
   152  			return true
   153  		}).WithPolling(pollingInterval).WithTimeout(waitTimeout).Should(BeTrue(), "Expected to find the old data")
   154  	})
   155  
   156  	// GIVEN a VZ environment with
   157  	// WHEN VZ custom resource is upgraded
   158  	// THEN only the system logs that are as old as the retention period
   159  	//      is migrated and older logs are purged
   160  	MinimumVerrazzanoIt("OpenSearch system logs older than retention period is not available post upgrade", func() {
   161  		systemRetentionPolicy, err := pkg.GetVerrazzanoRetentionPolicy(pkg.SystemLogIsmPolicyName)
   162  		if err != nil {
   163  			Fail("Error getting retention period for system logs from VZ CR - " + err.Error())
   164  		}
   165  		if systemRetentionPolicy.MinIndexAge == nil {
   166  			Skip("ISM policy configured for system logs does not have a retention period. Skipping the test")
   167  		}
   168  		oldLogsFound, err := pkg.ContainsDocsOlderThanRetentionPeriod(pkg.VerrazzanoNamespace, *systemRetentionPolicy.MinIndexAge)
   169  		if err != nil {
   170  			Fail("Error checking if docs older than retention period for system logs are present - " + err.Error())
   171  		}
   172  		Expect(oldLogsFound).To(Equal(false))
   173  	})
   174  
   175  	// GIVEN a VZ environment with
   176  	// WHEN VZ custom resource is upgraded
   177  	// THEN only the application logs that are as old as the retention period
   178  	//      is migrated and older logs are purged
   179  	MinimumVerrazzanoIt("OpenSearch application logs older than retention period is not available post upgrade", func() {
   180  		applicationRetentionPolicy, err := pkg.GetVerrazzanoRetentionPolicy(pkg.ApplicationLogIsmPolicyName)
   181  		if err != nil {
   182  			Fail("Error getting retention period for system logs from VZ CR - " + err.Error())
   183  		}
   184  		if applicationRetentionPolicy.MinIndexAge == nil {
   185  			Skip("ISM policy configured for application logs does not have a retention period. Skipping the test")
   186  		}
   187  		applicationDataStreams, err := pkg.GetApplicationDataStreamNames()
   188  		if err != nil {
   189  			Fail("Error getting all application data stream names - " + err.Error())
   190  		}
   191  		for _, applicationDataStream := range applicationDataStreams {
   192  			oldLogsFound, err := pkg.ContainsDocsOlderThanRetentionPeriod(applicationDataStream, *applicationRetentionPolicy.MinIndexAge)
   193  			if err != nil {
   194  				Fail("Error checking if older indices for application logs are present - " + err.Error())
   195  			}
   196  			Expect(oldLogsFound).To(Equal(false))
   197  		}
   198  	})
   199  
   200  	kubeConfigPath, err := k8sutil.GetKubeConfigLocation()
   201  	if err != nil {
   202  		Expect(err).To(BeNil(), fmt.Sprintf(pkg.KubeConfigErrorFmt, err))
   203  	}
   204  	t.ItMinimumVersion("Verify OpenSearch plugins have been installed", "1.6.0", kubeConfigPath, func() {
   205  		pkg.TestOpenSearchPlugins(pollingInterval, waitTimeout)
   206  	})
   207  
   208  })