Skip to content

Add missing unit tests for the operator#230

Merged
xgerman merged 3 commits intodocumentdb:mainfrom
hossain-rayhan:rayhan/operator-unit-test-update
Feb 13, 2026
Merged

Add missing unit tests for the operator#230
xgerman merged 3 commits intodocumentdb:mainfrom
hossain-rayhan:rayhan/operator-unit-test-update

Conversation

@hossain-rayhan
Copy link
Collaborator

Summary of New Unit Tests Created

  1. internal/cnpg/cnpg_cluster_test.go (NEW FILE)
    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:

Copilot AI review requested due to automatic review settings February 5, 2026 00:26
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.Contains instead 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

Rayhan Hossain 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>
@hossain-rayhan hossain-rayhan force-pushed the rayhan/operator-unit-test-update branch from 1d1fba8 to 7a5d6f2 Compare February 13, 2026 18:36
@xgerman xgerman merged commit e1d3b6e into documentdb:main Feb 13, 2026
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants