Skip to content

Commit e1131b7

Browse files
committed
chore(opensearch): use testcontainers.Run
1 parent 881156c commit e1131b7

File tree

1 file changed

+45
-54
lines changed

1 file changed

+45
-54
lines changed

modules/opensearch/opensearch.go

Lines changed: 45 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,16 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
3535

3636
// Run creates an instance of the OpenSearch container type
3737
func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*OpenSearchContainer, error) {
38-
req := testcontainers.ContainerRequest{
39-
Image: img,
40-
ExposedPorts: []string{defaultHTTPPort, "9600/tcp"},
41-
Env: map[string]string{
38+
moduleOpts := []testcontainers.ContainerCustomizer{
39+
testcontainers.WithExposedPorts(defaultHTTPPort, "9600/tcp"),
40+
testcontainers.WithEnv(map[string]string{
4241
"discovery.type": "single-node",
4342
"DISABLE_INSTALL_DEMO_CONFIG": "true",
4443
"DISABLE_SECURITY_PLUGIN": "true",
4544
"OPENSEARCH_USERNAME": defaultUsername,
4645
"OPENSEARCH_PASSWORD": defaultPassword,
47-
},
48-
HostConfigModifier: func(hc *container.HostConfig) {
46+
}),
47+
testcontainers.WithHostConfigModifier(func(hc *container.HostConfig) {
4948
hc.Ulimits = []*units.Ulimit{
5049
{
5150
Name: "memlock",
@@ -58,73 +57,65 @@ func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustom
5857
Hard: 65536,
5958
},
6059
}
61-
},
60+
}),
6261
}
6362

64-
genericContainerReq := testcontainers.GenericContainerRequest{
65-
ContainerRequest: req,
66-
Started: true,
67-
}
63+
moduleOpts = append(moduleOpts, opts...)
6864

6965
// Gather all config options (defaults and then apply provided options)
7066
settings := defaultOptions()
7167
for _, opt := range opts {
7268
if apply, ok := opt.(Option); ok {
7369
apply(settings)
7470
}
75-
if err := opt.Customize(&genericContainerReq); err != nil {
76-
return nil, err
77-
}
7871
}
7972

8073
// set credentials if they are provided, otherwise use the defaults
81-
if settings.Username != "" {
82-
genericContainerReq.Env["OPENSEARCH_USERNAME"] = settings.Username
83-
}
84-
if settings.Password != "" {
85-
genericContainerReq.Env["OPENSEARCH_PASSWORD"] = settings.Password
86-
}
87-
88-
username := genericContainerReq.Env["OPENSEARCH_USERNAME"]
89-
password := genericContainerReq.Env["OPENSEARCH_PASSWORD"]
74+
moduleOpts = append(moduleOpts, testcontainers.WithEnv(map[string]string{
75+
"OPENSEARCH_USERNAME": settings.Username,
76+
"OPENSEARCH_PASSWORD": settings.Password,
77+
}))
9078

9179
// the wat strategy does not support TLS at the moment,
9280
// so we need to disable it in the strategy for now.
93-
genericContainerReq.WaitingFor = wait.ForHTTP("/").
94-
WithPort("9200").
95-
WithTLS(false).
96-
WithStartupTimeout(120*time.Second).
97-
WithStatusCodeMatcher(func(status int) bool {
98-
return status == 200
99-
}).
100-
WithBasicAuth(username, password).
101-
WithResponseMatcher(func(body io.Reader) bool {
102-
bs, err := io.ReadAll(body)
103-
if err != nil {
104-
return false
105-
}
106-
107-
type response struct {
108-
Tagline string `json:"tagline"`
109-
}
110-
111-
var r response
112-
err = json.Unmarshal(bs, &r)
113-
if err != nil {
114-
return false
115-
}
116-
117-
return r.Tagline == "The OpenSearch Project: https://opensearch.org/"
118-
})
119-
120-
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
81+
moduleOpts = append(moduleOpts, testcontainers.WithAdditionalWaitStrategy(
82+
wait.ForHTTP("/").
83+
WithPort("9200").
84+
WithTLS(false).
85+
WithStartupTimeout(120*time.Second).
86+
WithStatusCodeMatcher(func(status int) bool {
87+
return status == 200
88+
}).
89+
WithBasicAuth(settings.Username, settings.Password).
90+
WithResponseMatcher(func(body io.Reader) bool {
91+
bs, err := io.ReadAll(body)
92+
if err != nil {
93+
return false
94+
}
95+
96+
type response struct {
97+
Tagline string `json:"tagline"`
98+
}
99+
100+
var r response
101+
err = json.Unmarshal(bs, &r)
102+
if err != nil {
103+
return false
104+
}
105+
106+
return r.Tagline == "The OpenSearch Project: https://opensearch.org/"
107+
}),
108+
),
109+
)
110+
111+
ctr, err := testcontainers.Run(ctx, img, moduleOpts...)
121112
var c *OpenSearchContainer
122-
if container != nil {
123-
c = &OpenSearchContainer{Container: container, User: username, Password: password}
113+
if ctr != nil {
114+
c = &OpenSearchContainer{Container: ctr, User: settings.Username, Password: settings.Password}
124115
}
125116

126117
if err != nil {
127-
return c, fmt.Errorf("generic container: %w", err)
118+
return c, fmt.Errorf("run: %w", err)
128119
}
129120

130121
return c, nil

0 commit comments

Comments
 (0)