Skip to content

Latest commit

 

History

History
292 lines (202 loc) · 11 KB

File metadata and controls

292 lines (202 loc) · 11 KB

ostesting

import "github.com/greenbone/opensight-golang-libraries/pkg/openSearch/ostesting"

Package ostesting provides a way to conveniently test against a real openSearch instance. It is in the same fashion of https://github.com/peterldowns/pgtestdb You need to have an OpenSearch instance running that the tests can connect to.

Index

Constants

KeepFailedEnv when set keeps the environment and test data after test execution failed. This is useful for debugging failed tests.

const KeepFailedEnv = "TEST_KEEP_FAILED"

func GetDocuments

func GetDocuments[T any](t *testing.T, tester *Tester, index string) []T

GetDocuments returns all documents added to test index

func GetDocumentsReturningError

func GetDocumentsReturningError[T any](tester *Tester, index string) ([]T, error)

GetDocumentsReturningError returns all documents added to test index and returns error (instead of failing the tests like GetDocuments or [GetTestTypeDocuments])

func RunNotParallelOption

func RunNotParallelOption(tst *Tester)

RunNotParallelOption signifies that tests associated with Tester should not be run in parallel (by default they are)

func ToAnySlice

func ToAnySlice[T any](input []T) []any

ToAnySlice converts a slice of any type to a slice of []any. Useful when passing documents to Tester.CreateDocuments.

type ClientConfig

type ClientConfig struct {
    Address  string
    User     string
    Password string
}

type TestType

TestType can be used as generic document object for testing

type TestType struct {
    ID               string    `json:"id"` // for easier identification in tests
    Text             string    `json:"text"`
    Keyword          string    `json:"keyword"`
    TextAndKeyword   string    `json:"textAndKeyword"`
    Integer          int       `json:"integer"`
    Float            float32   `json:"float"`
    Boolean          bool      `json:"boolean"`
    DateTimeStr      string    `json:"dateTimeStr,omitempty"`
    DateTime         time.Time `json:"dateTime"`
    KeywordOmitEmpty string    `json:"keywordOmitEmpty,omitempty"`
}

type Tester

Tester manages connecting with testing OpenSearch instance and implements helper methods

type Tester struct {
    // contains filtered or unexported fields
}

func NewTester

func NewTester(t *testing.T, opts ...TesterOption) *Tester

NewTester initializes new Tester It runs tests associated with [t] as parallel by default, unless runNotParallel option is provided.

func (Tester) Config

func (tst Tester) Config() ClientConfig

Config returns opensearch config that can be used to initialize client using test OpenSearch instance

func (Tester) CreateDocuments

func (tst Tester) CreateDocuments(t *testing.T, index string, docs []any, ids []string)

CreateDocuments creates documents [docs] on index [index] with IDs [ids]. If provided [ids] is nil the IDs for created documents will be generated.

func (Tester) CreateDocumentsReturningError

func (tst Tester) CreateDocumentsReturningError(index string, docs []any, ids []string) error

CreateDocumentsReturningError creates documents [docs] on index [index] with IDs [ids]. If provided [ids] is nil the IDs for created documents will be generated. Instead of using \*testing.T to fail on errors (like CreateDocuments) it returns error. Can be used when we expect error on creating documents in index and do not want to fail a test on it.

func (Tester) DeleteIndex

func (tst Tester) DeleteIndex(t *testing.T, index string)

DeleteIndex deletes indices with given name (or pattern, eg. "index-name*")

func (Tester) GetTestTypeDocuments

func (tst Tester) GetTestTypeDocuments(t *testing.T, index string) []TestType

GetTestTypeDocuments returns all documents of type TestType from [index]

func (Tester) NewIndex

func (tst Tester) NewIndex(t *testing.T, prefix string, mapping *string) string

NewIndex creates new index with unique name generated based on provided [prefix]. It returns the generated index name. Note: in most cases the _IndexAlias functions should be used that create both index and add it to alias as this represents the typical usage of indices (accessing them through alias) in the code.

func (Tester) NewIndexAlias

func (tst Tester) NewIndexAlias(t *testing.T, prefix string, mapping *string) (index, alias string)

NewIndexAlias creates new uniquely named index together with associated alias using mapping if not nil (otherwise with dynamic mapping). It also registers [t].Cleanup function that deletes the index after test.

[prefix] is the prefix name of the index that would be used to generate new unique index and alias name. Generation of unique index name is there to allow tests running in parallel and not interfere with each other.

Internally opensearchapi.Client is called directly rather than opensearch.Client, to avoid using tested object in testing setup and to not have to adjust opensearch.Client methods just for the sake of being used by Tester (as tester use-case of generating unique index/alias with custom mapping differs from production use-cases).

Upon successful creation the function returns index and associated alias names, in case of error [t] is used to mark test as failed. Note: usually the index should be accessed through alias name. In some cases the method needs to receive the concrete index instead.

func (Tester) NewNamedIndexAlias

func (tst Tester) NewNamedIndexAlias(t *testing.T, name string, mapping *string) string

NewNamedIndexAlias works like Tester.NewIndexAlias, except the name of the newly created index alias will be exactly as provided with [name] instead of being generated based on provided prefix. On successful creation it returns the concrete underlying index name It can be used in testing functionalities that rely on defined index alias name (eg. IndexVTS) and for which generated index alias name would not work. It goes with a drawback of having to make sure that other test run in parallel will not use the same index alias and interfere - while internal indices names are still unique, they would refer to the same alias.

func (Tester) NewTestTypeIndex

func (tst Tester) NewTestTypeIndex(t *testing.T, prefix string) string

NewTestTypeIndex creates new index appropriate to use with [testType] documents with given [prefix] of index name. Internally it calls Tester.NewIndex.

func (Tester) NewTestTypeIndexAlias

func (tst Tester) NewTestTypeIndexAlias(t *testing.T, prefix string) (index, alias string)

NewTestTypeIndexAlias creates new index appropriate to use with [testType] documents with given index and alias names [prefix] and returns new index and alias names. Internally it calls Tester.NewIndexAlias.

func (Tester) OSClient

func (tst Tester) OSClient() *opensearchapi.Client

OSClient returns [*opensearchapi.Client] associated with test OpenSearch instance

func (Tester) RefreshIndex

func (tst Tester) RefreshIndex(t *testing.T, index string)

RefreshIndex waits for index to refresh, so the updated documents can be obtained

func (Tester) T

func (tst Tester) T() *testing.T

T returns *testing.T associated with tester

type TesterOption

type TesterOption func(tst *Tester)

func WithAddress

func WithAddress(address string) TesterOption

WithAddress sets the custom address of testing opensearch instance to which the tester should point to

func WithConfig

func WithConfig(conf ClientConfig) TesterOption

WithConfig is an option to use custom ClientConfig for tester.

Generated by gomarkdoc