Skip to content

Commit f4e6f8b

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

6 files changed

Lines changed: 614 additions & 58 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: 10 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
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

910
import (
1011
"context"
11-
"crypto/tls"
1212
"encoding/json"
1313
"fmt"
1414
"io"
15-
"net/http"
1615
"os"
1716
"testing"
1817
"time"
@@ -37,7 +36,12 @@ func NewClient(t *testing.T) (*opensearchapi.Client, error) {
3736
if config != nil {
3837
client, err = opensearchapi.NewClient(*config)
3938
} else {
40-
client, err = opensearchapi.NewDefaultClient()
39+
// For insecure integration tests, explicitly use HTTP
40+
client, err = opensearchapi.NewClient(opensearchapi.Config{
41+
Client: opensearch.Config{
42+
Addresses: []string{"http://localhost:9200"},
43+
},
44+
})
4145
}
4246
if err != nil {
4347
return nil, err
@@ -117,59 +121,11 @@ func validateClusterReadiness(ctx context.Context, client *opensearchapi.Client)
117121
return nil
118122
}
119123

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-
171124
// GetVersion gets cluster info and returns version as int's
172125
func GetVersion(client *opensearchapi.Client) (int64, int64, int64, error) {
126+
if client == nil {
127+
return 0, 0, 0, fmt.Errorf("client cannot be nil")
128+
}
173129
resp, err := client.Info(context.Background(), nil)
174130
if err != nil {
175131
return 0, 0, 0, err

0 commit comments

Comments
 (0)