forked from opensearch-project/opensearch-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopensearch_integration_test.go
More file actions
473 lines (394 loc) · 12.6 KB
/
Copy pathopensearch_integration_test.go
File metadata and controls
473 lines (394 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// SPDX-License-Identifier: Apache-2.0
//
// The OpenSearch Contributors require contributions made to
// this file be licensed under the Apache-2.0 license or a
// compatible open source license.
//
// Modifications Copyright OpenSearch Contributors. See
// GitHub history for details.
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//go:build integration && core && !multinode
package opensearch_test
import (
"bytes"
"context"
"crypto/tls"
"errors"
"io"
"log"
"net"
"net/http"
"net/url"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/opensearch-project/opensearch-go/v5"
"github.com/opensearch-project/opensearch-go/v5/internal/build"
"github.com/opensearch-project/opensearch-go/v5/opensearchapi"
"github.com/opensearch-project/opensearch-go/v5/opensearchapi/testutil"
"github.com/opensearch-project/opensearch-go/v5/opensearchtransport"
"github.com/opensearch-project/opensearch-go/v5/opensearchtransport/testutil/mockhttp"
)
func TestClientTransport(t *testing.T) {
/*
t.Run("Persistent", func(t *testing.T) {
client, err := testutil.NewClient(t)
if err != nil {
t.Fatalf("Error creating the client: %s", err)
}
var total int
for i := 0; i < 101; i++ {
var curTotal int
res, err := client.Nodes.Stats(nil, &opensearchapi.NodesStatsReq{Metric: []string{"http"}})
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
for _, v := range res.Nodes {
curTotal = v.HTTP.TotalOpened
break
}
if curTotal < 1 {
t.Errorf("Unexpected total_opened: %d", curTotal)
}
if total == 0 {
total = curTotal
}
if total != curTotal {
t.Errorf("Expected total_opened=%d, got: %d", total, curTotal)
}
}
log.Printf("total_opened: %d", total)
})
*/
t.Run("Concurrent", func(t *testing.T) {
var wg sync.WaitGroup
client, err := testutil.InitClient(t)
require.NoError(t, err)
for range 101 {
time.Sleep(10 * time.Millisecond)
wg.Go(func() {
_, err := client.Info(t.Context(), nil)
require.NoError(t, err)
})
}
wg.Wait()
})
t.Run("WithContext", func(t *testing.T) {
client, err := testutil.InitClient(t)
require.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
defer cancel()
_, err = client.Info(ctx, nil)
require.Error(t, err, "Expected context deadline exceeded error")
})
t.Run("Configured", func(t *testing.T) {
tp := http.DefaultTransport.(*http.Transport).Clone()
tp.MaxIdleConnsPerHost = 10
tp.ResponseHeaderTimeout = time.Second
tp.DialContext = (&net.Dialer{Timeout: time.Nanosecond}).DialContext
tp.TLSClientConfig.MinVersion = tls.VersionTLS11
tp.TLSClientConfig.InsecureSkipVerify = true
cfg := opensearchapi.Config{
Client: opensearch.Config{
Context: t.Context(),
Transport: tp,
},
}
client, err := opensearchapi.NewClient(cfg)
require.NoError(t, err)
_, err = client.Info(t.Context(), nil)
require.Error(t, err)
opError := &net.OpError{}
if !errors.As(err, &opError) {
t.Fatalf("Expected net.OpError, but got: %T", err)
}
})
}
type CustomTransport struct {
client *http.Client
logger func(format string, v ...any)
}
func (t *CustomTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("X-Foo", "bar")
if t.logger != nil {
t.logger("> %s %q %q", req.Method, req.URL.String(), req.Header)
}
return t.client.Do(req)
}
func TestClientCustomTransport(t *testing.T) {
t.Run("Customized", func(t *testing.T) {
client, err := opensearchapi.NewDefaultClient()
require.NoError(t, err)
// Stop background pollers when the test finishes.
if closer, ok := client.Client.Transport.(io.Closer); ok {
t.Cleanup(func() { closer.Close() })
}
cfg := testutil.ClientConfig(t)
if cfg != nil {
customTP := http.DefaultTransport.(*http.Transport).Clone()
customTP.TLSClientConfig.InsecureSkipVerify = true
cfg.Client.Transport = &CustomTransport{
client: &http.Client{
Transport: customTP,
},
logger: func(format string, v ...any) {
if testutil.IsDebugEnabled(t) {
t.Logf(format, v...)
}
},
}
client, err = opensearchapi.NewClient(*cfg)
require.NoError(t, err)
// Wait for cluster to be ready before running tests
testutil.WaitForClusterReady(t, client)
}
// Simple readiness wait for manually-constructed client (only uses Info API)
ctx := t.Context()
for {
_, err := client.Info(ctx, nil)
if err == nil {
break
}
select {
case <-ctx.Done():
t.Fatalf("Cluster not ready: %s", ctx.Err())
case <-time.After(5 * time.Second):
// Retry
}
}
})
t.Run("Manual", func(t *testing.T) {
config := testutil.ClientConfig(t)
// Use centralized URL construction
u := mockhttp.GetOpenSearchURL(t)
tp, _ := opensearchtransport.New(opensearchtransport.Config{
URLs: []*url.URL{u},
Transport: config.Client.Transport,
Username: config.Client.Username,
Password: config.Client.Password,
Context: t.Context(),
})
client := opensearchapi.Client{
Client: &opensearch.Client{
Transport: tp,
},
}
// Simple readiness wait for manually-constructed client (only uses Info API)
ctx := t.Context()
for {
_, err := client.Info(ctx, nil)
if err == nil {
break
}
select {
case <-ctx.Done():
t.Fatalf("Cluster not ready: %s", ctx.Err())
case <-time.After(5 * time.Second):
// Retry
}
}
})
}
type TestTransport struct {
counter atomic.Uint64
t *testing.T
}
func (tr *TestTransport) Stream(req *http.Request) (*http.Response, error) {
// Use centralized URL construction
u := mockhttp.GetOpenSearchURL(tr.t)
req.URL.Scheme = u.Scheme
req.URL.Host = u.Host
config := testutil.ClientConfig(tr.t)
if testutil.IsSecure(tr.t) {
req.SetBasicAuth(config.Client.Username, config.Client.Password)
}
tr.counter.Add(1)
transport := config.Client.Transport
if transport == nil {
transport = http.DefaultTransport
}
return transport.RoundTrip(req)
}
func (tr *TestTransport) Count() uint64 {
return tr.counter.Load()
}
// Request buffers the response body so *TestTransport satisfies
// opensearchtransport.Interface alongside Stream.
func (tr *TestTransport) Request(req *http.Request) (*http.Response, error) {
res, err := tr.Stream(req)
if res != nil && res.Body != nil {
body, rerr := io.ReadAll(res.Body)
res.Body.Close()
res.Body = io.NopCloser(bytes.NewReader(body))
if rerr != nil && err == nil {
err = rerr
}
}
return res, err
}
func TestClientReplaceTransport(t *testing.T) {
t.Run("Replaced", func(t *testing.T) {
const expectedRequests = 10
tr := &TestTransport{t: t}
client := opensearchapi.Client{
Client: &opensearch.Client{
Transport: tr,
},
}
// Simple readiness wait for manually-constructed client (only uses Info API)
ctx := t.Context()
for {
_, err := client.Info(ctx, nil)
if err == nil {
break
}
select {
case <-ctx.Done():
t.Fatalf("Cluster not ready: %v", ctx.Err())
case <-time.After(5 * time.Second):
// Retry
}
}
// Reset counter after readiness check
initialCount := tr.Count()
for range expectedRequests {
_, err := client.Info(t.Context(), nil)
require.NoError(t, err)
}
actualRequests := tr.Count() - initialCount
if actualRequests > expectedRequests {
t.Errorf("Expected at most %d requests, got=%d", expectedRequests, actualRequests)
}
})
}
func TestClientAPI(t *testing.T) {
t.Run("Info", func(t *testing.T) {
client, err := testutil.NewClient(t)
require.NoError(t, err)
res, err := client.Info(t.Context(), nil)
require.NoError(t, err)
if res.ClusterName == "" {
log.Fatalf("cluster_name is empty: %s\n", err)
}
})
}
func TestClientGetConfigIntegration(t *testing.T) {
t.Run("GetConfig returns valid configuration", func(t *testing.T) {
// Get test config
cfg := testutil.ClientConfig(t)
// Create a client with specific configuration
osClient, err := opensearch.NewClient(cfg.Client)
require.NoError(t, err)
// Retrieve the config
retrievedConfig := osClient.GetConfig()
// Verify the config matches what was originally provided
require.Equal(t, cfg.Client.Addresses, retrievedConfig.Addresses)
require.Equal(t, cfg.Client.Username, retrievedConfig.Username)
require.Equal(t, cfg.Client.Password, retrievedConfig.Password)
})
t.Run("GetConfig with live client", func(t *testing.T) {
// Create a client from test helper
apiClient, err := testutil.NewClient(t)
require.NoError(t, err)
// Get config from the underlying opensearch client
config := apiClient.Client.GetConfig()
// Verify config has expected values
require.NotEmpty(t, config.Addresses, "addresses should not be empty")
// Verify we can create a new client with the retrieved config
newClient, err := opensearch.NewClient(*config)
require.NoError(t, err)
require.NotNil(t, newClient)
// Verify the new client works by making a request
req, err := build.Request(http.MethodGet, "/", nil, nil, nil)
require.NoError(t, err)
resp, err := newClient.Stream(req)
require.NoError(t, err)
require.NotNil(t, resp)
defer resp.Body.Close()
})
}
func TestNewFromClientIntegration(t *testing.T) {
t.Run("creates working api client from config", func(t *testing.T) {
// Get test config
cfg := testutil.ClientConfig(t)
// Create an opensearchapi.Client from the shared config
apiClient, err := opensearchapi.NewClient(opensearchapi.Config{Client: cfg.Client})
require.NoError(t, err)
require.NotNil(t, apiClient)
// Verify the api client can make requests
resp, err := apiClient.Info(t.Context(), nil)
require.NoError(t, err)
require.NotEmpty(t, resp)
require.NotEmpty(t, resp.ClusterName)
})
t.Run("api client shares config-derived transport with a base client", func(t *testing.T) {
// Get test config
cfg := testutil.ClientConfig(t)
// Create a base opensearch.Client and an api client from the same config
osClient, err := opensearch.NewClient(cfg.Client)
require.NoError(t, err)
apiClient, err := opensearchapi.NewClient(opensearchapi.Config{Client: cfg.Client})
require.NoError(t, err)
require.NotNil(t, apiClient.Client.Transport)
// Verify both clients can make requests successfully
req, err := build.Request(http.MethodGet, "/", nil, nil, nil)
require.NoError(t, err)
resp1, err := osClient.Stream(req)
require.NoError(t, err)
require.NotNil(t, resp1)
defer resp1.Body.Close()
resp2, err := apiClient.Info(t.Context(), nil)
require.NoError(t, err)
require.NotNil(t, resp2)
})
t.Run("maintains config through wrapped client", func(t *testing.T) {
// Get test config
cfg := testutil.ClientConfig(t)
// Create an opensearchapi.Client from the shared config
apiClient, err := opensearchapi.NewClient(opensearchapi.Config{Client: cfg.Client})
require.NoError(t, err)
// Retrieve config through the api client's wrapped opensearch client
retrievedConfig := apiClient.Client.GetConfig()
// Verify the config matches the original
require.Equal(t, cfg.Client.Addresses, retrievedConfig.Addresses)
require.Equal(t, cfg.Client.Username, retrievedConfig.Username)
require.Equal(t, cfg.Client.Password, retrievedConfig.Password)
})
t.Run("all sub-clients are functional", func(t *testing.T) {
// Get test config
cfg := testutil.ClientConfig(t)
// Create an opensearchapi.Client from the shared config
apiClient, err := opensearchapi.NewClient(opensearchapi.Config{Client: cfg.Client})
require.NoError(t, err)
// Test a few sub-clients to ensure they're properly initialized
// Cat client
catResp, err := apiClient.Cat.Health(t.Context(), nil)
require.NoError(t, err)
require.NotNil(t, catResp)
// Cluster client
clusterResp, err := apiClient.Cluster.Health(t.Context(), nil)
require.NoError(t, err)
require.NotNil(t, clusterResp)
// Nodes client
nodesResp, err := apiClient.Nodes.Info(t.Context(), nil)
require.NoError(t, err)
require.NotNil(t, nodesResp)
})
}