feat(azure): add cosmosdb module by natsoman · Pull Request #3452 · testcontainers/testcontainers-go
Summary by CodeRabbit
-
New Features
- Added Azure Cosmos DB emulator container provisioning to the Azure module, enabling local testing of Cosmos DB applications.
- Automatically generates connection strings and manages container endpoints for seamless integration with Cosmos DB clients.
- Includes example patterns demonstrating database and container creation workflows.
-
Documentation
- Added comprehensive Cosmos DB module documentation with configuration options and usage examples.
Walkthrough
This PR introduces Azure CosmosDB module support to testcontainers-go. It adds a Container type, Run() function for container orchestration, a ConnectionString() method for emulator access, and a custom policy for HTTP request routing. Tests and documentation demonstrate integration with the Azure CosmosDB SDK.
Changes
| Cohort / File(s) | Summary |
|---|---|
CosmosDB Container Implementation modules/azure/cosmosdb/cosmosdb.go |
New Container type wrapping testcontainers.Container; Run() function to create and configure CosmosDB containers with port 8081/tcp exposed and log-based wait strategy; ConnectionString() method to derive emulator endpoint and construct connection strings with predefined Cosmos DB Emulator key |
CosmosDB Policy modules/azure/cosmosdb/policy.go |
New ContainerPolicy type for HTTP request routing; NewContainerPolicy() to initialize with container endpoint; Do() method to override request host; ClientOptions() to integrate policy into azcosmos retry pipeline |
CosmosDB Tests & Examples modules/azure/cosmosdb/cosmosdb_test.go, modules/azure/cosmosdb/examples_test.go |
TestCosmosDB integration test demonstrating database and container creation with azcosmos client; ExampleRun() and ExampleRun_connect() example tests showing container startup and client integration patterns |
Documentation docs/modules/azure.md |
Adds CosmosDB section documenting Run function signature, container options, ConnectionString method, usage examples, and link in Azure module index |
Module Dependencies modules/azure/go.mod |
Upgrades azcore from v1.17.0 to v1.19.1; adds azcosmos v1.4.1, azblob v1.6.0, azqueue v1.0.0; updates azidentity and internal dependencies |
Sequence Diagram(s)
sequenceDiagram
participant User
participant Run as cosmosdb.Run()
participant Container as testcontainers
participant Policy as ContainerPolicy
participant Client as azcosmos.Client
User->>Run: Run(ctx, img, opts...)
activate Run
Run->>Container: Create container with port 8081/tcp
Run->>Container: Apply default + custom options
Container-->>Run: Container started
Run-->>User: *Container, nil
deactivate Run
User->>User: Create client from ConnectionString
activate User
User->>User: Call ConnectionString(ctx)
User->>Policy: NewContainerPolicy(ctx, container)
Policy-->>User: *ContainerPolicy with endpoint
User->>Client: Create client with policy
Client->>Client: Register policy in PerRetryPolicies
User->>Policy: Policy.Do(request) on each API call
activate Policy
Policy->>Policy: Override request.Host
Policy-->>Client: Forward to next policy
deactivate Policy
Client-->>User: Response
deactivate User
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~25 minutes
- Policy.Do() method: Verify HTTP request host override logic and error handling
- Error wrapping: Check context wrapping consistency across Container, ConnectionString, and policy functions
- azcosmos integration: Confirm ClientOptions embedding and PerRetryPolicies integration
- Port and endpoint derivation: Validate ConnectionString construction using PortEndpoint and constant Cosmos DB Emulator key
Possibly related PRs
- chore(azurite): use Run function #3318: Migrates other Azure modules (azurite, eventhubs, servicebus) to the same testcontainers.Run-style option composition pattern used in this CosmosDB implementation
Suggested reviewers
- mdelapenya
Poem
🐰 Cosmos twinkles from the cloud,
Now in containers, spinning proud,
With endpoints mapped and policies true,
The emulator dances through and through!
Testcontainers blooms—another day,
Azure's cosmos here to stay! 🌟
Pre-merge checks and finishing touches
✅ Passed checks (2 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description check | ✅ Passed | The pull request description is directly related to the changeset, explaining what the PR does (introduces CosmosDB module), why it matters (enables Go management of Cosmos DB), and how to test it (includes test operations). |
| Title check | ✅ Passed | The title clearly and specifically describes the main change: adding a new CosmosDB module to the Azure functionality. |
✨ Finishing touches
- 📝 Generate docstrings
🧪 Generate unit tests (beta)
- Create PR with unit tests
- Post copyable unit tests in a comment
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands and usage tips.