Add missing unit tests for the operator#230
Merged
xgerman merged 3 commits intodocumentdb:mainfrom Feb 13, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request adds comprehensive unit tests for the DocumentDB Kubernetes Operator, significantly improving test coverage across three key internal packages.
Changes:
- Added 3 new test files with extensive test coverage for CNPG cluster specification building, replication context handling, and utility functions
- Refactored existing tests to use standard library
strings.Containsinstead of custom helper functions - Added tests for previously untested utility functions including port resolution, image selection, environment-specific annotations, and service name generation
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| operator/src/internal/utils/util_test.go | Removed custom contains helper functions in favor of strings.Contains; added 8 new test functions covering port resolution, image selection, annotations, service name generation, and service IP validation |
| operator/src/internal/utils/replication_context_test.go | New test file with 13 test functions covering all ReplicationContext methods including state checks, networking strategies, service name generation, and cluster name generation |
| operator/src/internal/cnpg/cnpg_cluster_test.go | New test file with 4 test functions covering CNPG cluster specification generation, inherited metadata labels, bootstrap configuration, and stop delay settings |
added 3 commits
February 13, 2026 10:28
Signed-off-by: Rayhan Hossain <rhossain@microsoft.com>
Signed-off-by: Rayhan Hossain <rhossain@microsoft.com>
Signed-off-by: Rayhan Hossain <rhossain@microsoft.com>
1d1fba8 to
7a5d6f2
Compare
alaye-ms
approved these changes
Feb 13, 2026
xgerman
approved these changes
Feb 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary of New Unit Tests Created
Tests for the CNPG cluster specification builder:
TestGetCnpgClusterSpec - Tests cluster creation with various configurations (basic, custom sidecar, TLS ready, custom gateway image)
TestGetInheritedMetadataLabels - Tests label generation for standard names, special characters, and empty names
TestGetBootstrapConfiguration - Tests bootstrap with InitDB vs Recovery scenarios
TestGetMaxStopDelayOrDefault - Tests default and custom stop delay values
2. internal/utils/replication_context_test.go (NEW FILE)
Comprehensive tests for the ReplicationContext struct:
TestReplicationContext_IsPrimary - NoReplication, Primary, Replica states
TestReplicationContext_IsReplicating - All replication states
TestReplicationContext_GetReplicationSource - Source determination logic
TestReplicationContext_EndpointEnabled - Endpoint enablement scenarios
TestReplicationContext_IsAzureFleetNetworking - Azure Fleet detection
TestReplicationContext_IsIstioNetworking - Istio detection
TestReplicationContext_String - String representation
TestReplicationContext_CreateStandbyNamesList - Standby list generation
TestReplicationContext_GenerateExternalClusterServices - External service generation
TestReplicationContext_GenerateIncomingServiceNames - Incoming service names
TestReplicationContext_GenerateOutgoingServiceNames - Outgoing service names
TestGenerateCNPGClusterName - Cluster name generation with length constraints
3. internal/utils/util_test.go (EXPANDED)
Added missing tests:
TestGetPortFor - Port resolution for postgres, sidecar, gateway, and unknown ports
TestGetGatewayImageForDocumentDB - Gateway image selection logic
TestGetDocumentDBImageForInstance - DocumentDB image selection logic
TestGetEnvironmentSpecificAnnotations - Cloud-specific annotations (EKS, AKS, GKE)
TestGenerateServiceName_PublicFunction - Service name generation with truncation
TestEnsureServiceIP - Service IP validation for various service types
TestGetDocumentDBServiceDefinition_LoadBalancerAnnotations - LoadBalancer annotation logic
TestGetDocumentDBServiceDefinition_ServiceNameLength - Service name length constraints
Test Results
All tests pass successfully: