Skip to content

Commit d8a1f62

Browse files
committed
Migrate API and plugin tests to context-aware calls
Replace nil and context.Background() with t.Context() throughout all API and plugin test files to enable proper test timeout handling and allow Go's test framework to clean up resources when tests are terminated or timeout. Test improvements: - opensearchapi: All 37 API test files migrated to t.Context() - opensearchutil: JSON reader integration test updated - plugins/ism: Policy and API tests migrated - plugins/security: All 12 security plugin tests migrated Context propagation enables proper test timeout handling, respects test deadlines, and allows Go's test runner to perform cleanup when tests are cancelled. This makes tests more resilient and easier to debug when they fail or timeout. Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
1 parent ccd6ccd commit d8a1f62

52 files changed

Lines changed: 1088 additions & 817 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
77
### Added
88

99
- Enhanced cluster readiness checking for improved test reliability: `ostest.NewClient()` now includes readiness validation (health + cluster state + nodes info)
10+
- Test parallelization support via TEST_PARALLEL environment variable (default: CPU cores - 1, minimum 1)
11+
- opensearchutil/testutil package with PollUntil helper for eventual consistency testing (ISM policies, index readiness, cluster state changes)
1012
- Configuration option `IncludeDedicatedClusterManagers` for controlling cluster manager node routing ([#765](https://github.com/opensearch-project/opensearch-go/issues/765))
1113
- Policy-based routing system for improved request routing and service availability ([#771](https://github.com/opensearch-project/opensearch-go/pull/771))
1214
- `Policy` interface for composable routing strategies with lifecycle management
@@ -26,17 +28,34 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
2628
- Refactor Client struct to use embedded mutex pattern for improved thread safety ([#775](https://github.com/opensearch-project/opensearch-go/pull/775))
2729
- Refactor metrics struct to use atomic counters for lock-free request/failure tracking ([#776](https://github.com/opensearch-project/opensearch-go/pull/776))
2830
- Test against Opensearch 2.19.4, 3.1, 3.3, and 3.4 ([#782](https://github.com/opensearch-project/opensearch-go/pull/782))
31+
- Migrate all test files to context-aware API calls for proper timeout and cancellation support
32+
- Add cluster readiness validation and improve cluster error diagnostics
33+
- Update Docker cluster management to add version-aware role detection (cluster_manager vs master)
34+
- Generate unique document IDs in tests for parallel test execution and eliminate known test flakes
35+
- Reduce integration test timeout from 1h to 10m per package with parallel execution support
36+
- Refactor transport code for improved maintainability (rename ErrInvalidRole -> InvalidRoleError, add response body cleanup, simplify initialization)
2937
- **BREAKING**: Enhanced node discovery to match OpenSearch server behavior ([#765](https://github.com/opensearch-project/opensearch-go/issues/765))
3038
- Dedicated cluster manager nodes are now excluded from client request routing by default (best practice)
3139
- Node selection logic now matches Java client `NodeSelector.SKIP_DEDICATED_CLUSTER_MASTERS` behavior
40+
- **BREAKING**: Add context support to discovery and client lifecycle management
41+
- `opensearchtransport.Discoverable` interface now requires `context.Context` parameter: `DiscoverNodes(ctx context.Context) error`
42+
- `opensearch.Client.DiscoverNodes()` and `opensearchtransport.Client.DiscoverNodes()` now require `context.Context` parameter
43+
- `opensearch.Config` and `opensearchtransport.Config` now accept optional `Context` and `CancelFunc` fields
44+
- `opensearchutil.BulkIndexerConfig` now accepts optional `Context` and `CancelFunc` fields
45+
- Enables proper context propagation for timeouts, cancellation, and graceful shutdown
3246

3347
### Deprecated
3448

3549
### Removed
3650

3751
### Fixed
3852

53+
- Fix connection lifecycle bug in statusConnectionPool.OnFailure where connections were scheduled for resurrection before being moved from live to dead list, causing potential race conditions
3954
- Fix flaky connection integration test by replacing arbitrary sleep times with proper server readiness polling
55+
- Fix cluster readiness checks in integration tests to handle HTTPS cold start delays (increase timeout to 15s)
56+
- Fix GitHub Actions workflow authentication for OpenSearch 2.12.0+ password changes (admin -> myStrongPassword123!)
57+
- Fix Docker cluster management to properly handle version-specific configurations and clean stale images/volumes
58+
- Fix OpenSearch 2.8.0+ Tasks API compatibility by adding cancellation_time_millis field to TasksListTask struct
4059
- Fix OpenSearch 3.1.0+ API compatibility by adding phase_results_processors field to nodes API and time_in_execution fields to cluster pending tasks API
4160
- Fix OpenSearch 3.2.0+ API compatibility by adding max_last_index_request_timestamp and startree query fields across nodes stats, indices stats, and cat APIs, plus settings field to security plugin health API
4261
- Fix OpenSearch 3.3.0+ API compatibility by adding neural_search breaker, query_failed and startree_query_failed search fields, search pipeline system_generated fields across multiple APIs, plus ingestion_status field to cluster state API and jwks_uri field to security config API

opensearchapi/api_aliases_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,30 @@ import (
1717
ostest "github.com/opensearch-project/opensearch-go/v4/internal/test"
1818
"github.com/opensearch-project/opensearch-go/v4/opensearchapi"
1919
osapitest "github.com/opensearch-project/opensearch-go/v4/opensearchapi/internal/test"
20+
"github.com/opensearch-project/opensearch-go/v4/opensearchutil/testutil"
2021
)
2122

2223
func TestAliases(t *testing.T) {
2324
t.Run("Aliases", func(t *testing.T) {
2425
client, err := ostest.NewClient(t)
2526
require.Nil(t, err)
2627

27-
index := "test-aliases"
28+
index := testutil.MustUniqueString(t, "test-aliases")
2829
t.Cleanup(func() {
29-
client.Indices.Delete(nil, opensearchapi.IndicesDeleteReq{Indices: []string{index}})
30+
client.Indices.Delete(t.Context(), opensearchapi.IndicesDeleteReq{Indices: []string{index}})
3031
})
3132

32-
_, err = client.Indices.Create(nil, opensearchapi.IndicesCreateReq{Index: index})
33+
_, err = client.Indices.Create(t.Context(), opensearchapi.IndicesCreateReq{Index: index})
3334
require.Nil(t, err)
3435

3536
t.Run("with request", func(t *testing.T) {
3637
resp, err := client.Aliases(
37-
nil,
38+
t.Context(),
3839
opensearchapi.AliasesReq{
39-
Body: strings.NewReader(`{"actions":[{"add":{"index":"test-aliases","alias":"logs"}},{"remove":{"index":"test-aliases","alias":"logs"}}]}`),
40+
Body: strings.NewReader(
41+
`{"actions":[{"add":{"index":"` + index + `","alias":"logs"}},` +
42+
`{"remove":{"index":"` + index + `","alias":"logs"}}]}`,
43+
),
4044
},
4145
)
4246
require.Nil(t, err)
@@ -49,7 +53,7 @@ func TestAliases(t *testing.T) {
4953
failingClient, err := osapitest.CreateFailingClient()
5054
require.Nil(t, err)
5155

52-
res, err := failingClient.Aliases(nil, opensearchapi.AliasesReq{})
56+
res, err := failingClient.Aliases(t.Context(), opensearchapi.AliasesReq{})
5357
require.NotNil(t, err)
5458
require.NotNil(t, res)
5559
osapitest.VerifyInspect(t, res.Inspect())

opensearchapi/api_bulk_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestBulkClient(t *testing.T) {
2727

2828
index := "test-bulk"
2929
t.Cleanup(func() {
30-
client.Indices.Delete(nil, opensearchapi.IndicesDeleteReq{Indices: []string{index}})
30+
client.Indices.Delete(t.Context(), opensearchapi.IndicesDeleteReq{Indices: []string{index}})
3131
})
3232

3333
tests := []struct {
@@ -45,7 +45,12 @@ func TestBulkClient(t *testing.T) {
4545
Name: "without index",
4646
Request: opensearchapi.BulkReq{
4747
Body: strings.NewReader(
48-
fmt.Sprintf("{\"index\": {\"_index\": \"%s\"}}\n{\"test\": 1234}\n{\"create\": {\"_index\": \"%s\"}}\n{\"test\": 5678}\n", index, index),
48+
fmt.Sprintf(
49+
"{\"index\": {\"_index\": \"%s\"}}\n{\"test\": 1234}\n"+
50+
"{\"create\": {\"_index\": \"%s\"}}\n{\"test\": 5678}\n",
51+
index,
52+
index,
53+
),
4954
),
5055
},
5156
},
@@ -54,7 +59,7 @@ func TestBulkClient(t *testing.T) {
5459
for _, test := range tests {
5560
t.Run(test.Name, func(t *testing.T) {
5661
res, err := client.Bulk(
57-
nil,
62+
t.Context(),
5863
test.Request,
5964
)
6065
require.Nil(t, err)
@@ -66,7 +71,7 @@ func TestBulkClient(t *testing.T) {
6671
failingClient, err := osapitest.CreateFailingClient()
6772
require.Nil(t, err)
6873

69-
res, err := failingClient.Bulk(nil, opensearchapi.BulkReq{Index: index})
74+
res, err := failingClient.Bulk(t.Context(), opensearchapi.BulkReq{Index: index})
7075
assert.NotNil(t, err)
7176
assert.NotNil(t, res)
7277
osapitest.VerifyInspect(t, res.Inspect())

0 commit comments

Comments
 (0)