…riven coverage, refactoring, and regression prevention:
- Add InternalOpenSearchSinkIntegrationTestConcreteIndex: covers the index
creation path with a plain concrete date-based index (no alias)
- Extract shared tests into AbstractInternalOpenSearchSinkIntegrationTest
using Template Method pattern: testPersistsAuditEventsToTarget and
testAuditDocumentContainsMandatoryFields now run against both concrete-index
and alias variants without duplication
- Refactor InternalOpenSearchSinkIntegrationTestAuditAlias: now extends the
abstract base, removing 3 duplicate methods (countAuditDocs, generateAuditEvent,
testAuditDocumentsViaAliasContainMandatoryFields); retains only the specialized
test testRecognizesAuditTargetAsWriteAlias
- Add InternalOpenSearchSinkTest unit tests: covers race condition
(ResourceAlreadyExistsException), generic cluster failures, and
acknowledged=false scenarios that cannot be tested reliably on
single-node embedded cluster
- Fix resource leak in testCreatesAuditIndexAutomatically
Closes opensearch-project#6145
Signed-off-by: Pietro Paolo Castagna <PietroPaolo.Castagna@gmail.com>
Summary
Adds integration and unit tests covering previously untested branches in
InternalOpenSearchSink,and refactors test classes to eliminate duplication using the Template Method pattern.
This improves overall robustness and helps prevent regressions.
Problem & Solution
Existing State
The codebase contains
InternalOpenSearchSinkIntegrationTestAuditAlias, an integration testthat covers the
metadata.hasAlias() == truebranch with a real cluster configured with a write alias.Coverage Gap
The complementary path—where the sink must create a concrete index with automatic date-based naming—
was not tested. Additionally, cluster-level failure scenarios (race conditions, acknowledged=false,
generic exceptions) could not be reliably reproduced on a single-node embedded cluster.
Solution
Three coordinated changes eliminate duplication and complete coverage:
New integration test:
InternalOpenSearchSinkIntegrationTestConcreteIndexexercises index creation on a plain cluster without aliases, covering the default path where
indices follow the
security-auditlog-YYYY.MM.ddpattern.Extract commonality:
AbstractInternalOpenSearchSinkIntegrationTestholds the shared test methods (
testPersistsAuditEventsToTarget,testAuditDocumentContainsMandatoryFields)that are valid regardless of cluster configuration. Both concrete-index and alias variants
inherit and reuse these tests through method overrides (
cluster(),auditTarget()),eliminating 68 lines of duplicate code.
Unit test hard-to-reproduce scenarios:
InternalOpenSearchSinkTestuses Mockito to cover branches that cannot be reliably triggered on an embedded cluster:
ResourceAlreadyExistsExceptionmust be treated as successChanges
New Test Classes
InternalOpenSearchSinkIntegrationTestConcreteIndex:
covers the index creation path with a plain, unaliased cluster; verifies that indices
are created with the default date-based pattern (security-auditlog-YYYY.MM.dd)
InternalOpenSearchSinkTest:
unit tests with Mockito covering non-reproducible cluster scenarios:
race conditions (ResourceAlreadyExistsException), generic cluster failures,
and unacknowledged responses
Refactored Test Classes
AbstractInternalOpenSearchSinkIntegrationTest:
extracted shared integration tests that work against both concrete-index and alias variants;
subclasses override
cluster()andauditTarget()to inject their configuration.Eliminates duplication; each variant now contributes only its specialized behavior.
InternalOpenSearchSinkIntegrationTestAuditAlias:
refactored to extend the abstract base; removed 3 duplicate methods
(countAuditDocs, generateAuditEvent, testAuditDocumentsViaAliasContainMandatoryFields);
now focuses solely on its unique test:
testRecognizesAuditTargetAsWriteAliasTesting
All existing tests continue to pass. New tests provide complete branch coverage
for
InternalOpenSearchSink.createIndexIfAbsent().Issues Resolved
Closes #6145
Check List
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.