fead: Add default container connection string provider#1630
fead: Add default container connection string provider#1630HofmeisterAn merged 4 commits intodevelopfrom
Conversation
✅ Deploy Preview for testcontainers-dotnet ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
WalkthroughAdds a generic ContainerConnectionStringProvider base class with Configure/GetConnectionString logic, three new connection-string exceptions, unit tests for provider behaviors, a small Dockerfile example comment update, and dependency bumps (Moq, xUnit packages). Changes
Sequence Diagram(s)sequenceDiagram
participant Caller as Caller
participant Provider as ContainerConnectionStringProvider
participant Container as TContainerEntity
participant Config as TConfigurationEntity
Caller->>Provider: Configure(container, configuration)
Provider->>Provider: store Container & Configuration
Caller->>Provider: GetConnectionString(mode)
alt mode == Host
Provider->>Provider: GetHostConnectionString()
Provider-->>Caller: host connection string or throw ConnectionStringNotAvailableException
else mode == Container
Provider->>Provider: GetContainerConnectionString()
Provider-->>Caller: container connection string or throw ConnectionStringModeNotSupportedException
else
Provider-->>Caller: throw ConnectionStringModeNotSupportedException
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@Directory.Packages.props`:
- Line 29: The xunit.v3 package was bumped to 3.2.2 but the
xunit.v3.extensibility.core PackageVersion remains at 3.1.0; update the
PackageVersion element for xunit.v3.extensibility.core to Version="3.2.2" so it
matches xunit.v3 (ensure the PackageVersion
Include="xunit.v3.extensibility.core" entry is changed), and scan for any other
xunit.v3.* PackageVersion entries to keep them in sync with 3.2.2.
🧹 Nitpick comments (4)
src/Testcontainers/Containers/ConnectionStringNotAvailableException.cs (1)
18-19: Inconsistent constructor parameter order compared to sibling exceptions.
ConnectionStringModeNotSupportedExceptiontakes(connectionMode, providerType)while this takes(providerType, connectionMode). Consider aligning the parameter order across all three exception types for API consistency, since these are new public types.tests/Testcontainers.Platform.Linux.Tests/ContainerConnectionStringProviderTest.cs (1)
28-36: Consider adding anullname test alongside the empty-string test.
GetConnectionString(string name, ...)usesstring.IsNullOrEmpty(name), sonullis also a valid fallback path. A quick additional[Fact]withprovider.GetConnectionString(null)would make the coverage more thorough.src/Testcontainers/Configurations/Containers/ContainerConnectionStringProvider`2.cs (2)
17-22:ContainerandConfigurationarenulluntilConfigureis called.Subclass implementations of
GetHostConnectionString()/GetContainerConnectionString()will almost certainly access these properties. IfGetConnectionString()is invoked beforeConfigure(), the subclass will hit aNullReferenceExceptionwith no clear diagnostic message. Consider adding a guard at the top ofGetConnectionString(ConnectionMode)(or a shared helper) to throw early with a meaningful message, e.g.:Proposed guard
public virtual string GetConnectionString(ConnectionMode connectionMode = ConnectionMode.Host) { + if (Container == null || Configuration == null) + { + throw new InvalidOperationException( + $"The connection string provider '{GetType().Name}' has not been configured. Call Configure() before accessing connection strings."); + } + switch (connectionMode) {
25-29: No null-checks onConfigureparameters.If
nullis passed for either argument, the failure is deferred toGetConnectionString()time, making it harder to diagnose. A simple guard-clause would fail fast at the point of misconfiguration.
What does this PR do?
The
IContainerinterface implementsIConnectionStringProvider, which provides access to the configured connection string provider. Until now, Testcontainers modules have not implemented connection string providers. This can be misleading for users, because they expectGetConnectionString()to return a module-specific connection string. Instead, calling this method currently throws an exception since no connection string provider is configured. For example, when working with Consul, developers must callGetBaseAddress()instead, which is not very intuitive.Why is it important?
To make it easier and more convenient for developers to get started with Testcontainers and to avoid running into this issue, the PR introduces a
ContainerConnectionStringProviderimplementation. Modules can use this to delegateGetConnectionString()to their equivalent method. For example, the Consul module can delegate toGetBaseAddress().Related issues
-
Summary by CodeRabbit
New Features
Bug Fixes / Reliability
Documentation
Tests
Chores