Skip to content

Commit 7349a01

Browse files
committed
fix internal/test commit miss
Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
1 parent 54d08f6 commit 7349a01

5 files changed

Lines changed: 154 additions & 5 deletions

File tree

internal/test/config.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
7+
package ostest
8+
9+
import (
10+
"os"
11+
12+
"github.com/opensearch-project/opensearch-go/v4"
13+
)
14+
15+
// IsSecure returns true when SECURE_INTEGRATION env is set to true
16+
func IsSecure() bool {
17+
return os.Getenv("SECURE_INTEGRATION") == "true"
18+
}
19+
20+
// GetPassword returns the password suited for the opensearch version
21+
func GetPassword() (string, error) {
22+
var (
23+
major, minor int64
24+
err error
25+
)
26+
password := "admin"
27+
version := os.Getenv("OPENSEARCH_VERSION")
28+
29+
if version != "latest" && version != "" {
30+
major, minor, _, err = opensearch.ParseVersion(version)
31+
if err != nil {
32+
return "", err
33+
}
34+
if version == "latest" || major > 2 || (major == 2 && minor >= 12) {
35+
password = "myStrongPassword123!"
36+
}
37+
} else {
38+
password = "myStrongPassword123!"
39+
}
40+
return password, nil
41+
}

internal/test/doc.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
7+
// Package ostest provides OpenSearch testing utilities and infrastructure.
8+
// Integration-specific functionality is available only when building with integration tags.
9+
package ostest

internal/test/helper.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// The OpenSearch Contributors require contributions made to
44
// this file be licensed under the Apache-2.0 license or a
55
// compatible open source license.
6+
//go:build integration
67

78
package ostest
89

internal/test/readiness_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// compatible open source license.
66
//go:build integration
77

8-
package ostest_test
8+
package ostest
99

1010
import (
1111
"context"
@@ -14,13 +14,11 @@ import (
1414
"github.com/stretchr/testify/assert"
1515
"github.com/stretchr/testify/require"
1616
"github.com/stretchr/testify/suite"
17-
18-
ostest "github.com/opensearch-project/opensearch-go/v4/internal/test"
1917
)
2018

2119
// TestNewClient demonstrates the enhanced client creation with automatic readiness checks
2220
func TestNewClient(t *testing.T) {
23-
client, err := ostest.NewClient(t)
21+
client, err := NewClient(t)
2422
require.NoError(t, err, "Failed to create client")
2523
require.NotNil(t, client, "Client should not be nil")
2624

@@ -40,7 +38,7 @@ func TestNewClient(t *testing.T) {
4038

4139
// ExampleTestSuite demonstrates using the testify suite pattern
4240
type ExampleTestSuite struct {
43-
ostest.OpenSearchTestSuite
41+
OpenSearchTestSuite
4442
}
4543

4644
func (s *ExampleTestSuite) TestClusterHealthWithSuite() {

internal/test/suite.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)