-
-
Notifications
You must be signed in to change notification settings - Fork 342
feat: Add Toxiproxy module #1454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
HofmeisterAn
merged 17 commits into
testcontainers:develop
from
iltertaha:feature/toxiproxy-module
Nov 8, 2025
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b9d94c9
Create toxiproxy module template
iltertaha 5879ba5
Add initial implementation
iltertaha a533564
Remove signature check for local builds
iltertaha 9a7ba96
Add initial tests
iltertaha 6312313
Add Toxiproxy support and refactor tests
iltertaha 2c47e3d
Merge branch 'develop' into feature/toxiproxy-module
iltertaha 96a9cc4
Revert omitted assembly key file
iltertaha 138b1c8
Update target framework for Toxiproxy and add projects to the sln
iltertaha d5e4304
Add toxiproxy doc and links, update pipeline steps
iltertaha da88b57
Merge remote-tracking branch 'origin/develop' into fork/iltertaha/fea…
HofmeisterAn 7615785
chore: Add necessary .runs-on config file
HofmeisterAn 72d467b
foo
HofmeisterAn 1c7d354
chore: Align with repo standards
HofmeisterAn cb9dbdd
chore: Apply code review
HofmeisterAn ec338e6
docs: Add module docs
HofmeisterAn 3a590ab
chore: Apply code review
HofmeisterAn a8e742e
chore: Remove empy line in code comments
HofmeisterAn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # Toxiproxy | ||
|
|
||
| [Toxiproxy](https://github.com/Shopify/toxiproxy) is a proxy to simulate network and system conditions for chaos and resiliency testing. It can simulate latency, timeouts, bandwidth limits, and connection issues between services. | ||
|
|
||
| Add the following dependency to your project file: | ||
|
|
||
| ```shell title="NuGet" | ||
| dotnet add package Testcontainers.Toxiproxy | ||
| ``` | ||
|
|
||
| You can start a Toxiproxy container instance from any .NET application. This example demonstrates how to test network conditions by proxying traffic through Toxiproxy to a Redis container. The test creates both containers in the same network and configures Toxiproxy to redirect traffic from the test host to the Redis container. | ||
|
|
||
| === "Create Container Instance" | ||
| ```csharp | ||
| --8<-- "tests/Testcontainers.Toxiproxy.Tests/ToxiproxyContainerTest.cs:CreateToxiproxyContainer" | ||
| ``` | ||
|
|
||
| This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the containers. Both the Redis and Toxiproxy containers are started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the containers are removed in the `DisposeAsync` method. | ||
|
|
||
| === "Usage Example" | ||
| ```csharp | ||
| --8<-- "tests/Testcontainers.Toxiproxy.Tests/ToxiproxyContainerTest.cs:UseToxiproxyContainer" | ||
| ``` | ||
|
|
||
| ## How it works | ||
|
|
||
| To test network conditions with Toxiproxy, you need to configure the communication between your test host, Toxiproxy, and the service you want to test: | ||
|
|
||
| 1. Place both containers in the same network so the Toxiproxy container and the service container (Redis in this example) can communicate with each other. | ||
|
|
||
| 2. Configure the Toxiproxy proxy to redirect traffic from the test host to the service container. The proxy's `Listen` address specifies where Toxiproxy listens for incoming connections, and the `Upstream` address specifies the target service. | ||
|
|
||
| === "Proxy Configuration" | ||
| ```csharp | ||
| --8<-- "tests/Testcontainers.Toxiproxy.Tests/ToxiproxyContainerTest.cs:ProxyConfiguration" | ||
| ``` | ||
|
|
||
| 3. Connect through Toxiproxy using its hostname and one of its proxied ports. The Toxiproxy module initializes `32` ports starting from `8666` that can be used to configure proxies and redirect traffic. In the example, the Redis connection uses the Toxiproxy container's hostname and port instead of connecting directly to Redis. | ||
|
|
||
| === "Connect Through Toxiproxy" | ||
| ```csharp | ||
| --8<-- "tests/Testcontainers.Toxiproxy.Tests/ToxiproxyContainerTest.cs:ConnectThroughToxiproxy" | ||
| ``` | ||
|
|
||
| 4. Apply toxics to the proxy to simulate network conditions. For example, a latency toxic to the downstream traffic. | ||
|
|
||
| === "Toxic Configuration" | ||
| ```csharp | ||
| --8<-- "tests/Testcontainers.Toxiproxy.Tests/ToxiproxyContainerTest.cs:ToxicConfiguration" | ||
| ``` | ||
|
|
||
| !!! tip | ||
| Toxiproxy allows you to configure the `Toxicity` property, which determines the probability (0.0 to 1.0) that a toxic will be applied to the connection. A value of 1.0 means the toxic is applied 100% of the time, while 0.5 would apply it to approximately 50% of requests. | ||
|
|
||
| The test example uses the following NuGet dependencies: | ||
|
|
||
| === "Package References" | ||
| ```xml | ||
| --8<-- "tests/Testcontainers.Toxiproxy.Tests/Testcontainers.Toxiproxy.Tests.csproj:PackageReferences" | ||
| ``` | ||
|
|
||
| To execute the tests, use the command `dotnet test` from a terminal. | ||
|
|
||
| --8<-- "docs/modules/_call_out_test_projects.txt" |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| root = true |
12 changes: 12 additions & 0 deletions
12
src/Testcontainers.Toxiproxy/Testcontainers.Toxiproxy.csproj
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TargetFrameworks>net8.0;net9.0;netstandard2.0;netstandard2.1</TargetFrameworks> | ||
| <LangVersion>latest</LangVersion> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="JetBrains.Annotations" VersionOverride="2023.3.0" PrivateAssets="All"/> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="../Testcontainers/Testcontainers.csproj"/> | ||
| </ItemGroup> | ||
| </Project> |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| namespace Testcontainers.Toxiproxy; | ||
|
|
||
| /// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" /> | ||
| [PublicAPI] | ||
| public sealed class ToxiproxyBuilder : ContainerBuilder<ToxiproxyBuilder, ToxiproxyContainer, ToxiproxyConfiguration> | ||
| { | ||
| public const string ToxiproxyImage = "ghcr.io/shopify/toxiproxy:2.12.0"; | ||
|
|
||
| public const ushort ToxiproxyControlPort = 8474; | ||
|
|
||
| public const ushort FirstProxiedPort = 8666; | ||
|
|
||
| public const ushort LastProxiedPort = 8666 + 32; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ToxiproxyBuilder" /> class. | ||
| /// </summary> | ||
| public ToxiproxyBuilder() | ||
| : this(new ToxiproxyConfiguration()) | ||
| { | ||
| DockerResourceConfiguration = Init().DockerResourceConfiguration; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ToxiproxyBuilder" /> class. | ||
| /// </summary> | ||
| /// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
| private ToxiproxyBuilder(ToxiproxyConfiguration resourceConfiguration) | ||
| : base(resourceConfiguration) | ||
| { | ||
| DockerResourceConfiguration = resourceConfiguration; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override ToxiproxyConfiguration DockerResourceConfiguration { get; } | ||
|
|
||
| /// <inheritdoc /> | ||
| public override ToxiproxyContainer Build() | ||
| { | ||
| Validate(); | ||
| return new ToxiproxyContainer(DockerResourceConfiguration); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override ToxiproxyBuilder Init() | ||
| { | ||
| const int count = LastProxiedPort - FirstProxiedPort; | ||
|
|
||
| var toxiproxyBuilder = base.Init() | ||
| .WithImage(ToxiproxyImage) | ||
| .WithPortBinding(ToxiproxyControlPort, true) | ||
| .WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request => | ||
| request.ForPath("/version").ForPort(ToxiproxyControlPort))); | ||
|
|
||
| // Allows up to 32 ports to be proxied (arbitrary value). The ports are | ||
| // exposed here, but whether Toxiproxy listens on them is controlled at | ||
| // runtime when configuring the proxy. | ||
| return Enumerable.Range(FirstProxiedPort, count) | ||
| .Aggregate(toxiproxyBuilder, (builder, port) => | ||
| builder.WithPortBinding(port, true)); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override ToxiproxyBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) | ||
| { | ||
| return Merge(DockerResourceConfiguration, new ToxiproxyConfiguration(resourceConfiguration)); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override ToxiproxyBuilder Clone(IContainerConfiguration resourceConfiguration) | ||
| { | ||
| return Merge(DockerResourceConfiguration, new ToxiproxyConfiguration(resourceConfiguration)); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override ToxiproxyBuilder Merge(ToxiproxyConfiguration oldValue, ToxiproxyConfiguration newValue) | ||
| { | ||
| return new ToxiproxyBuilder(new ToxiproxyConfiguration(oldValue, newValue)); | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| namespace Testcontainers.Toxiproxy; | ||
|
|
||
| /// <inheritdoc cref="ContainerConfiguration" /> | ||
| [PublicAPI] | ||
| public sealed class ToxiproxyConfiguration : ContainerConfiguration | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ToxiproxyConfiguration" /> class. | ||
| /// </summary> | ||
| public ToxiproxyConfiguration() | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ToxiproxyConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
| public ToxiproxyConfiguration(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) | ||
| : base(resourceConfiguration) | ||
| { | ||
| // Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ToxiproxyConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
| public ToxiproxyConfiguration(IContainerConfiguration resourceConfiguration) | ||
| : base(resourceConfiguration) | ||
| { | ||
| // Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ToxiproxyConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
| public ToxiproxyConfiguration(ToxiproxyConfiguration resourceConfiguration) | ||
| : this(new ToxiproxyConfiguration(), resourceConfiguration) | ||
| { | ||
| // Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ToxiproxyConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="oldValue">The old Docker resource configuration.</param> | ||
| /// <param name="newValue">The new Docker resource configuration.</param> | ||
| public ToxiproxyConfiguration(ToxiproxyConfiguration oldValue, ToxiproxyConfiguration newValue) | ||
| : base(oldValue, newValue) | ||
| { | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| namespace Testcontainers.Toxiproxy; | ||
|
|
||
| /// <inheritdoc cref="DockerContainer" /> | ||
| [PublicAPI] | ||
| public sealed class ToxiproxyContainer : DockerContainer | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ToxiproxyContainer" /> class. | ||
| /// </summary> | ||
| /// <param name="configuration">The container configuration.</param> | ||
| public ToxiproxyContainer(ToxiproxyConfiguration configuration) | ||
| : base(configuration) | ||
| { | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| global using System.Linq; | ||
| global using Docker.DotNet.Models; | ||
| global using DotNet.Testcontainers.Builders; | ||
| global using DotNet.Testcontainers.Configurations; | ||
| global using DotNet.Testcontainers.Containers; | ||
| global using JetBrains.Annotations; |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| root = true |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ubuntu-24.04 |
23 changes: 23 additions & 0 deletions
23
tests/Testcontainers.Toxiproxy.Tests/Testcontainers.Toxiproxy.Tests.csproj
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TargetFrameworks>net9.0</TargetFrameworks> | ||
| <IsPackable>false</IsPackable> | ||
| <IsPublishable>false</IsPublishable> | ||
| <OutputType>Exe</OutputType> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <!-- -8<- [start:PackageReferences] --> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk"/> | ||
| <PackageReference Include="coverlet.collector"/> | ||
| <PackageReference Include="xunit.runner.visualstudio"/> | ||
| <PackageReference Include="xunit.v3"/> | ||
| <PackageReference Include="Toxiproxy.Net"/> | ||
| <PackageReference Include="StackExchange.Redis"/> | ||
| <!-- -8<- [end:PackageReferences] --> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="../../src/Testcontainers.Toxiproxy/Testcontainers.Toxiproxy.csproj"/> | ||
| <ProjectReference Include="../../src/Testcontainers.Redis/Testcontainers.Redis.csproj"/> | ||
| <ProjectReference Include="../Testcontainers.Commons/Testcontainers.Commons.csproj"/> | ||
| </ItemGroup> | ||
| </Project> |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.