|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// |
| 3 | +// The OpenSearch Contributors require contributions made to |
| 4 | +// this file be licensed under the Apache-2.0 license or a |
| 5 | +// compatible open source license. |
| 6 | +//go:build integration |
| 7 | + |
| 8 | +package ostest |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + "fmt" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + "github.com/stretchr/testify/suite" |
| 16 | + |
| 17 | + "github.com/opensearch-project/opensearch-go/v4/opensearchapi" |
| 18 | +) |
| 19 | + |
| 20 | +// OpenSearchTestSuite provides a testify suite with automatic client setup and readiness checking |
| 21 | +type OpenSearchTestSuite struct { |
| 22 | + suite.Suite |
| 23 | + Client *opensearchapi.Client |
| 24 | + |
| 25 | + // Version information for test logic |
| 26 | + Major, Minor, Patch int64 |
| 27 | +} |
| 28 | + |
| 29 | +// SetupSuite is called once before any tests in the suite run |
| 30 | +func (s *OpenSearchTestSuite) SetupSuite() { |
| 31 | + t := s.T() |
| 32 | + |
| 33 | + // Create client with automatic readiness checking |
| 34 | + client, err := NewClient(t) |
| 35 | + require.NoError(t, err, "Failed to create OpenSearch client") |
| 36 | + s.Client = client |
| 37 | + |
| 38 | + // Get and store version information for test use |
| 39 | + major, minor, patch, err := GetVersion(s.Client) |
| 40 | + require.NoError(t, err, "Failed to get OpenSearch version") |
| 41 | + |
| 42 | + s.Major, s.Minor, s.Patch = major, minor, patch |
| 43 | +} |
| 44 | + |
| 45 | +// TearDownSuite is called once after all tests in the suite have run |
| 46 | +func (s *OpenSearchTestSuite) TearDownSuite() { |
| 47 | + // Currently no cleanup needed for the client |
| 48 | +} |
| 49 | + |
| 50 | +// SetupTest is called before each individual test |
| 51 | +func (s *OpenSearchTestSuite) SetupTest() { |
| 52 | + // Ensure cluster is still healthy before each test |
| 53 | + ctx := context.Background() |
| 54 | + _, err := s.Client.Cluster.Health(ctx, nil) |
| 55 | + require.NoError(s.T(), err, "Cluster health check failed before test") |
| 56 | +} |
| 57 | + |
| 58 | +// SkipIfBelowVersion skips the current test if the cluster version is below the specified version |
| 59 | +func (s *OpenSearchTestSuite) SkipIfBelowVersion(majorVersion, patchVersion int64, testName string) { |
| 60 | + if s.Major < majorVersion || (s.Major == majorVersion && s.Patch < patchVersion) { |
| 61 | + s.T().Skipf("Skipping %s test as it requires OpenSearch %d.x.%d+, current version: %d.%d.%d", |
| 62 | + testName, majorVersion, patchVersion, s.Major, s.Minor, s.Patch) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// SkipIfNotSecure skips the current test if running against an insecure cluster |
| 67 | +func (s *OpenSearchTestSuite) SkipIfNotSecure() { |
| 68 | + if !IsSecure() { |
| 69 | + s.T().Skip("Skipping test as it requires a secured cluster") |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// Version returns the OpenSearch version as a formatted string |
| 74 | +func (s *OpenSearchTestSuite) Version() string { |
| 75 | + return fmt.Sprintf("%d.%d.%d", s.Major, s.Minor, s.Patch) |
| 76 | +} |
| 77 | + |
| 78 | +// RequireHealthyCluster ensures the cluster is in a healthy state |
| 79 | +func (s *OpenSearchTestSuite) RequireHealthyCluster() { |
| 80 | + ctx := context.Background() |
| 81 | + resp, err := s.Client.Cluster.Health(ctx, nil) |
| 82 | + require.NoError(s.T(), err, "Failed to get cluster health") |
| 83 | + require.NotNil(s.T(), resp, "Cluster health response is nil") |
| 84 | +} |
| 85 | + |
| 86 | +// CleanupIndex ensures an index is deleted, useful for test cleanup |
| 87 | +func (s *OpenSearchTestSuite) CleanupIndex(indexName string) { |
| 88 | + ctx := context.Background() |
| 89 | + _, _ = s.Client.Indices.Delete(ctx, opensearchapi.IndicesDeleteReq{ |
| 90 | + Indices: []string{indexName}, |
| 91 | + }) |
| 92 | +} |
| 93 | + |
| 94 | +// EnsureIndex creates an index if it doesn't exist, useful for test setup |
| 95 | +func (s *OpenSearchTestSuite) EnsureIndex(indexName string) { |
| 96 | + ctx := context.Background() |
| 97 | + _, _ = s.Client.Indices.Create(ctx, opensearchapi.IndicesCreateReq{ |
| 98 | + Index: indexName, |
| 99 | + }) |
| 100 | +} |
0 commit comments