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.
- Constants
- func GetDocuments[T any](t *testing.T, tester *Tester, index string) []T
- func GetDocumentsReturningError[T any](tester *Tester, index string) ([]T, error)
- func RunNotParallelOption(tst *Tester)
- func ToAnySlice[T any](input []T) []any
- type ClientConfig
- type TestType
- type Tester
- func NewTester(t *testing.T, opts ...TesterOption) *Tester
- func (tst Tester) Config() ClientConfig
- func (tst Tester) CreateDocuments(t *testing.T, index string, docs []any, ids []string)
- func (tst Tester) CreateDocumentsReturningError(index string, docs []any, ids []string) error
- func (tst Tester) DeleteIndex(t *testing.T, index string)
- func (tst Tester) GetTestTypeDocuments(t *testing.T, index string) []TestType
- func (tst Tester) NewIndex(t *testing.T, prefix string, mapping *string) string
- func (tst Tester) NewIndexAlias(t *testing.T, prefix string, mapping *string) (index, alias string)
- func (tst Tester) NewNamedIndexAlias(t *testing.T, name string, mapping *string) string
- func (tst Tester) NewTestTypeIndex(t *testing.T, prefix string) string
- func (tst Tester) NewTestTypeIndexAlias(t *testing.T, prefix string) (index, alias string)
- func (tst Tester) OSClient() *opensearchapi.Client
- func (tst Tester) RefreshIndex(t *testing.T, index string)
- func (tst Tester) T() *testing.T
- type TesterOption
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[T any](t *testing.T, tester *Tester, index string) []TGetDocuments returns all documents added to test index
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(tst *Tester)RunNotParallelOption signifies that tests associated with Tester should not be run in parallel (by default they are)
func ToAnySlice[T any](input []T) []anyToAnySlice converts a slice of any type to a slice of []any. Useful when passing documents to Tester.CreateDocuments.
type ClientConfig struct {
Address string
User string
Password string
}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"`
}Tester manages connecting with testing OpenSearch instance and implements helper methods
type Tester struct {
// contains filtered or unexported fields
}func NewTester(t *testing.T, opts ...TesterOption) *TesterNewTester initializes new Tester It runs tests associated with [t] as parallel by default, unless runNotParallel option is provided.
func (tst Tester) Config() ClientConfigConfig returns opensearch config that can be used to initialize client using test OpenSearch instance
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 (tst Tester) CreateDocumentsReturningError(index string, docs []any, ids []string) errorCreateDocumentsReturningError 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 (tst Tester) DeleteIndex(t *testing.T, index string)DeleteIndex deletes indices with given name (or pattern, eg. "index-name*")
func (tst Tester) GetTestTypeDocuments(t *testing.T, index string) []TestTypeGetTestTypeDocuments returns all documents of type TestType from [index]
func (tst Tester) NewIndex(t *testing.T, prefix string, mapping *string) stringNewIndex 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 (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 (tst Tester) NewNamedIndexAlias(t *testing.T, name string, mapping *string) stringNewNamedIndexAlias 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 (tst Tester) NewTestTypeIndex(t *testing.T, prefix string) stringNewTestTypeIndex creates new index appropriate to use with [testType] documents with given [prefix] of index name. Internally it calls Tester.NewIndex.
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 (tst Tester) OSClient() *opensearchapi.ClientOSClient returns [*opensearchapi.Client] associated with test OpenSearch instance
func (tst Tester) RefreshIndex(t *testing.T, index string)RefreshIndex waits for index to refresh, so the updated documents can be obtained
func (tst Tester) T() *testing.TT returns *testing.T associated with tester
type TesterOption func(tst *Tester)func WithAddress(address string) TesterOptionWithAddress sets the custom address of testing opensearch instance to which the tester should point to
func WithConfig(conf ClientConfig) TesterOptionWithConfig is an option to use custom ClientConfig for tester.
Generated by gomarkdoc