github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/logging/opensearch/opensearch_mappings_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  	dump "github.com/verrazzano/verrazzano/tests/e2e/pkg/test/clusterdump"
     9  	"time"
    10  
    11  	. "github.com/onsi/ginkgo/v2"
    12  	. "github.com/onsi/gomega"
    13  	"github.com/verrazzano/verrazzano/pkg/k8sutil"
    14  	"github.com/verrazzano/verrazzano/tests/e2e/pkg"
    15  	"github.com/verrazzano/verrazzano/tests/e2e/pkg/test/framework"
    16  )
    17  
    18  const (
    19  	shortPollingInterval = 10 * time.Second
    20  	shortWaitTimeout     = 1 * time.Minute
    21  	longWaitTimeout      = 15 * time.Minute
    22  	indexDocumentURL     = "%s/_doc"
    23  )
    24  
    25  var t = framework.NewTestFramework("field-mappings")
    26  
    27  var failed = false
    28  var _ = t.AfterEach(func() {
    29  	failed = failed || CurrentSpecReport().Failed()
    30  })
    31  
    32  var afterSuite = t.AfterSuiteFunc(func() {
    33  	if failed {
    34  		dump.ExecuteBugReport()
    35  	}
    36  	pkg.DeleteApplicationDataStream("verrazzano-application-test")
    37  })
    38  
    39  var _ = AfterSuite(afterSuite)
    40  
    41  var _ = t.Describe("OpenSearch field mappings", Label("f:observability.logging.es"), func() {
    42  	// It Wrapper to only run spec if component is supported on the current Verrazzano installation
    43  	MinimumVerrazzanoIt := func(description string, f func()) {
    44  		kubeconfigPath, err := k8sutil.GetKubeConfigLocation()
    45  		if err != nil {
    46  			t.It(description, func() {
    47  				Fail(fmt.Sprintf("Failed to get default kubeconfig path: %s", err.Error()))
    48  			})
    49  		}
    50  		supported, err := pkg.IsVerrazzanoMinVersion("1.3.0", kubeconfigPath)
    51  		if err != nil {
    52  			t.It(description, func() {
    53  				Fail(err.Error())
    54  			})
    55  		}
    56  		// Only run tests if Verrazzano is at least version 1.3.0
    57  		// And data streams are created
    58  		if supported && pkg.IsDataStreamSupported() {
    59  			t.It(description, f)
    60  		} else {
    61  			pkg.Log(pkg.Info, fmt.Sprintf("Skipping check '%v', Verrazzano is not at version 1.3.0 or data streams are not created", description))
    62  		}
    63  	}
    64  	MinimumVerrazzanoIt("Documents with non-object fields get stored as strings", func() {
    65  		// GIVEN OpenSearch verrazzano application index
    66  		// WHEN the documents with same field name but different data types are written
    67  		// THEN verify that both the docs are written successfully
    68  		var indexName string
    69  		var err error
    70  		Eventually(func() error {
    71  			indexName, err = pkg.GetOpenSearchAppIndex("test")
    72  			return err
    73  		}, shortWaitTimeout, shortPollingInterval).Should(BeNil(), "Expected to get OpenSearch App Index")
    74  
    75  		Eventually(func() bool {
    76  			doc1 := `{"key":2,"@timestamp":"2022-03-15T19:55:54Z"}`
    77  			resp, err := pkg.PostOpensearch(fmt.Sprintf(indexDocumentURL, indexName), doc1)
    78  			if err != nil {
    79  				pkg.Log(pkg.Error, fmt.Sprintf("Failed to write to OpenSearch: %v", err))
    80  				return false
    81  			}
    82  			if resp.StatusCode != 201 {
    83  				pkg.Log(pkg.Error, fmt.Sprintf("Failed to write to OpenSearch: status=%d: body=%s", resp.StatusCode,
    84  					string(resp.Body)))
    85  				return false
    86  			}
    87  			kubeconfigPath, err := k8sutil.GetKubeConfigLocation()
    88  			if err != nil {
    89  				pkg.Log(pkg.Error, fmt.Sprintf("Failed to get default kubeconfig path: %s", err.Error()))
    90  				return false
    91  			}
    92  			supported, err := pkg.IsVerrazzanoMinVersion("1.3.0", kubeconfigPath)
    93  			if err != nil {
    94  				pkg.Log(pkg.Error, fmt.Sprintf("Error getting Verrazzano version: %v", err))
    95  				return false
    96  			}
    97  			doc2 := `{"key":"text","@timestamp":"2022-03-15T19:55:54Z"}`
    98  			resp, err = pkg.PostOpensearch(fmt.Sprintf(indexDocumentURL, indexName), doc2)
    99  			if err != nil {
   100  				pkg.Log(pkg.Error, fmt.Sprintf("Failed to write a document to OpenSearch: %v", err))
   101  				return false
   102  			}
   103  			if supported {
   104  				if resp.StatusCode != 201 {
   105  					pkg.Log(pkg.Error, fmt.Sprintf("Failed to write to OpenSearch with a different data type field: "+
   106  						"'status=%d: body=%s", resp.StatusCode, string(resp.Body)))
   107  					return false
   108  				}
   109  			} else {
   110  				if resp.StatusCode != 400 {
   111  					pkg.Log(pkg.Error, fmt.Sprintf("Excepted to fail to write to OpenSearch: status=%d: body=%s",
   112  						resp.StatusCode, string(resp.Body)))
   113  					return false
   114  				}
   115  			}
   116  			return true
   117  		}).WithPolling(shortPollingInterval).WithTimeout(longWaitTimeout).Should(BeTrue(), "Expected to write data successfully to OpenSearch with different data types")
   118  	})
   119  
   120  	MinimumVerrazzanoIt("Documents with object fields get stored as objects", func() {
   121  		// GIVEN OpenSearch verrazzano application index
   122  		// WHEN the documents with same field name but one with object and the other one with concrete value are written
   123  		// THEN verify that the second document insertion fails
   124  		var indexName string
   125  		var err error
   126  		Eventually(func() error {
   127  			indexName, err = pkg.GetOpenSearchAppIndex("test")
   128  			return err
   129  		}, shortWaitTimeout, shortPollingInterval).Should(BeNil(), "Expected to get OpenSearch App Index")
   130  
   131  		Eventually(func() bool {
   132  			doc1 := `{"keyObject":{"name":"unit-test-cluster"},"@timestamp":"2022-03-15T19:55:54Z"}`
   133  			resp, err := pkg.PostOpensearch(fmt.Sprintf(indexDocumentURL, indexName), doc1)
   134  			if err != nil {
   135  				pkg.Log(pkg.Error, fmt.Sprintf("Failed to write to OpenSearch: %v", err))
   136  				return false
   137  			}
   138  			if resp.StatusCode != 201 {
   139  				pkg.Log(pkg.Error, fmt.Sprintf("Failed to write to OpenSearch: status=%d: body=%s", resp.StatusCode,
   140  					string(resp.Body)))
   141  				return false
   142  			}
   143  			doc2 := `{"keyObject":"text","@timestamp":"2022-03-15T19:55:54Z"}`
   144  			resp, err = pkg.PostOpensearch(fmt.Sprintf(indexDocumentURL, indexName), doc2)
   145  			if err != nil {
   146  				pkg.Log(pkg.Error, fmt.Sprintf("Failed to write another document to OpenSearch: %v", err))
   147  				return false
   148  			}
   149  			if resp.StatusCode != 400 {
   150  				pkg.Log(pkg.Error, fmt.Sprintf("Excepted to fail to write to OpenSearch: status=%d: body=%s",
   151  					resp.StatusCode, string(resp.Body)))
   152  				return false
   153  			}
   154  			return true
   155  		}, longWaitTimeout, shortPollingInterval).Should(BeTrue(), "Expected to fail writing data with concrete value for object field in OpenSearch")
   156  	})
   157  })