Skip to content

Commit 8bef159

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

5 files changed

Lines changed: 180 additions & 56 deletions

File tree

internal/test/config.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
"crypto/tls"
11+
"net/http"
12+
"os"
13+
14+
"github.com/opensearch-project/opensearch-go/v4"
15+
"github.com/opensearch-project/opensearch-go/v4/opensearchapi"
16+
)
17+
18+
// IsSecure returns true when SECURE_INTEGRATION env is set to true
19+
func IsSecure() bool {
20+
return os.Getenv("SECURE_INTEGRATION") == "true"
21+
}
22+
23+
// ClientConfig returns an opensearchapi.Config for secure opensearch
24+
func ClientConfig() (*opensearchapi.Config, error) {
25+
if IsSecure() {
26+
password, err := GetPassword()
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
return &opensearchapi.Config{
32+
Client: opensearch.Config{
33+
Username: "admin",
34+
Password: password,
35+
Addresses: []string{"https://localhost:9200"},
36+
Transport: &http.Transport{
37+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
38+
},
39+
},
40+
}, nil
41+
}
42+
//nolint:nilnil // easier to test with nil rather then doing complex error handling for tests
43+
return nil, nil
44+
}
45+
46+
// GetPassword returns the password suited for the opensearch version
47+
func GetPassword() (string, error) {
48+
var (
49+
major, minor int64
50+
err error
51+
)
52+
password := "admin"
53+
version := os.Getenv("OPENSEARCH_VERSION")
54+
55+
if version != "latest" && version != "" {
56+
major, minor, _, err = opensearch.ParseVersion(version)
57+
if err != nil {
58+
return "", err
59+
}
60+
if version == "latest" || major > 2 || (major == 2 && minor >= 12) {
61+
password = "myStrongPassword123!"
62+
}
63+
} else {
64+
password = "myStrongPassword123!"
65+
}
66+
return password, nil
67+
}

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 & 51 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

@@ -117,57 +118,6 @@ func validateClusterReadiness(ctx context.Context, client *opensearchapi.Client)
117118
return nil
118119
}
119120

120-
// IsSecure returns true when SECURE_INTEGRATION env is set to true
121-
func IsSecure() bool {
122-
return os.Getenv("SECURE_INTEGRATION") == "true"
123-
}
124-
125-
// ClientConfig returns an opensearchapi.Config for secure opensearch
126-
func ClientConfig() (*opensearchapi.Config, error) {
127-
if IsSecure() {
128-
password, err := GetPassword()
129-
if err != nil {
130-
return nil, err
131-
}
132-
133-
return &opensearchapi.Config{
134-
Client: opensearch.Config{
135-
Username: "admin",
136-
Password: password,
137-
Addresses: []string{"https://localhost:9200"},
138-
Transport: &http.Transport{
139-
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
140-
},
141-
},
142-
}, nil
143-
}
144-
//nolint:nilnil // easier to test with nil rather then doing complex error handling for tests
145-
return nil, nil
146-
}
147-
148-
// GetPassword returns the password suited for the opensearch version
149-
func GetPassword() (string, error) {
150-
var (
151-
major, minor int64
152-
err error
153-
)
154-
password := "admin"
155-
version := os.Getenv("OPENSEARCH_VERSION")
156-
157-
if version != "latest" && version != "" {
158-
major, minor, _, err = opensearch.ParseVersion(version)
159-
if err != nil {
160-
return "", err
161-
}
162-
if version == "latest" || major > 2 || (major == 2 && minor >= 12) {
163-
password = "myStrongPassword123!"
164-
}
165-
} else {
166-
password = "myStrongPassword123!"
167-
}
168-
return password, nil
169-
}
170-
171121
// GetVersion gets cluster info and returns version as int's
172122
func GetVersion(client *opensearchapi.Client) (int64, int64, int64, error) {
173123
resp, err := client.Info(context.Background(), nil)

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)