Skip to content

fead: Add default container connection string provider#1630

Merged
HofmeisterAn merged 4 commits intodevelopfrom
feature/add-default-connection-string-provider
Feb 7, 2026
Merged

fead: Add default container connection string provider#1630
HofmeisterAn merged 4 commits intodevelopfrom
feature/add-default-connection-string-provider

Conversation

@HofmeisterAn
Copy link
Collaborator

@HofmeisterAn HofmeisterAn commented Feb 7, 2026

What does this PR do?

The IContainer interface implements IConnectionStringProvider, 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 expect GetConnectionString() 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 call GetBaseAddress() 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 ContainerConnectionStringProvider implementation. Modules can use this to delegate GetConnectionString() to their equivalent method. For example, the Consul module can delegate to GetBaseAddress().

Related issues

-

Summary by CodeRabbit

  • New Features

    • Added connection-string provider infrastructure with Host and Container modes and configuration support.
  • Bug Fixes / Reliability

    • New error types for unsupported modes/names and missing connection strings to improve diagnostics.
  • Documentation

    • Updated Docker base image example in docs.
  • Tests

    • Added unit tests covering provider behaviors.
  • Chores

    • Updated test-related package versions and added a mocking dependency.

@HofmeisterAn HofmeisterAn added the enhancement New feature or request label Feb 7, 2026
@netlify
Copy link

netlify bot commented Feb 7, 2026

Deploy Preview for testcontainers-dotnet ready!

Name Link
🔨 Latest commit 031dfe2
🔍 Latest deploy log https://app.netlify.com/projects/testcontainers-dotnet/deploys/69871619a12198000733ed30
😎 Deploy Preview https://deploy-preview-1630--testcontainers-dotnet.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai
Copy link

coderabbitai bot commented Feb 7, 2026

Walkthrough

Adds 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

Cohort / File(s) Summary
Dependency & Test Project
Directory.Packages.props, tests/Testcontainers.Platform.Linux.Tests/Testcontainers.Platform.Linux.Tests.csproj, tests/Testcontainers.Platform.Linux.Tests/Usings.cs
Bumped Cake.Frosting, xunit.analyzers, xunit.v3; added Moq and a global using for Moq in the Linux test project.
Connection String Provider
src/Testcontainers/Configurations/Containers/ContainerConnectionStringProvider\2.cs`
New generic abstract base implementing IConnectionStringProvider: Configure, GetConnectionString overloads, ConnectionMode switch (Host/Container), hooks for host/container strings, and required-string validation.
Connection String Exceptions
src/Testcontainers/Containers/ConnectionStringModeNotSupportedException.cs, src/Testcontainers/Containers/ConnectionStringNameNotSupportedException.cs, src/Testcontainers/Containers/ConnectionStringNotAvailableException.cs
Added three sealed exception types covering unsupported modes, unsupported names, and missing connection strings.
Tests
tests/Testcontainers.Platform.Linux.Tests/ContainerConnectionStringProviderTest.cs
New unit tests: configuration exposure, host-only and host+container behaviors, named-string unsupported errors, and missing-string errors using test doubles.
Docs/example tweak
src/Testcontainers/Images/DockerfileArchive.cs
Updated example Dockerfile FROM comment from .NET SDK 6.0 to 10.0 (documentation-only change).

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
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐇 I nibble strings from host to crate,
I store the things you configure straight,
Tests hop in to check each case,
Exceptions warn with measured pace,
Packages updated — carrots on my plate 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.91% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: introducing a default container connection string provider, which is the core feature of this PR.
Description check ✅ Passed The PR description adequately covers the 'What does this PR do?' and 'Why is it important?' sections with clear explanations of the motivation and implementation approach.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/add-default-connection-string-provider

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

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.

ConnectionStringModeNotSupportedException takes (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 a null name test alongside the empty-string test.

GetConnectionString(string name, ...) uses string.IsNullOrEmpty(name), so null is also a valid fallback path. A quick additional [Fact] with provider.GetConnectionString(null) would make the coverage more thorough.

src/Testcontainers/Configurations/Containers/ContainerConnectionStringProvider`2.cs (2)

17-22: Container and Configuration are null until Configure is called.

Subclass implementations of GetHostConnectionString() / GetContainerConnectionString() will almost certainly access these properties. If GetConnectionString() is invoked before Configure(), the subclass will hit a NullReferenceException with no clear diagnostic message. Consider adding a guard at the top of GetConnectionString(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 on Configure parameters.

If null is passed for either argument, the failure is deferred to GetConnectionString() time, making it harder to diagnose. A simple guard-clause would fail fast at the point of misconfiguration.

@HofmeisterAn HofmeisterAn merged commit 9de8428 into develop Feb 7, 2026
148 checks passed
@HofmeisterAn HofmeisterAn deleted the feature/add-default-connection-string-provider branch February 7, 2026 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant