Skip to content
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

fix: Add HTTP wait strategy to prevent race-condition in WaitUntilHttpRequestIsSucceededTest #1299

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ branches:
# Required. Require branches to be up to date before merging.
strict: true
# Required. The list of status checks to require in order to merge into this branch
contexts: ["analyze (csharp)", "build (ubuntu-22.04)", "build (windows-2022)", "netlify/testcontainers-dotnet/deploy-preview"]
contexts: ["Test Report", "analyze (csharp)"]
# Required. Enforce all configured restrictions for administrators. Set to true to enforce required status checks for repository administrators. Set to null to disable.
enforce_admins: false
# Prevent merge commits from being pushed to matching branches
Expand Down Expand Up @@ -207,7 +207,7 @@ branches:
# Required. Require branches to be up to date before merging.
strict: true
# Required. The list of status checks to require in order to merge into this branch
contexts: ["analyze (csharp)", "build (ubuntu-22.04)", "build (windows-2022)", "netlify/testcontainers-dotnet/deploy-preview"]
contexts: ["Test Report", "analyze (csharp)"]
# Required. Enforce all configured restrictions for administrators. Set to true to enforce required status checks for repository administrators. Set to null to disable.
enforce_admins: false
# Prevent merge commits from being pushed to matching branches
Expand Down
2 changes: 2 additions & 0 deletions tests/Testcontainers.Commons/CommonImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public static class CommonImages

public static readonly IImage Alpine = new DockerImage("alpine:3.17");

public static readonly IImage Socat = new DockerImage("alpine/socat:1.8.0.0");

public static readonly IImage Curl = new DockerImage("curlimages/curl:8.00.1");

public static readonly IImage Nginx = new DockerImage("nginx:1.22");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,12 @@ public sealed class HttpFixture : IAsyncLifetime
private const ushort HttpPort = 80;

private readonly IContainer _container = new ContainerBuilder()
.WithImage(CommonImages.Alpine)
.WithEntrypoint("/bin/sh", "-c")
.WithCommand($"while true; do echo \"HTTP/1.1 200 OK\r\n\r\n\" | nc -l -p {HttpPort}; done")
.WithImage(CommonImages.Socat)
.WithCommand("-v")
.WithCommand($"TCP-LISTEN:{HttpPort},crlf,reuseaddr,fork")
.WithCommand("EXEC:\"echo -e 'HTTP/1.1 200 OK'\n\n\"")
.WithPortBinding(HttpPort, true)
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request =>
request.ForPath("/")))
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request => request))
.Build();

public string BaseAddress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ public sealed class WaitUntilHttpRequestIsSucceededTest : IAsyncLifetime
private const ushort HttpPort = 80;

private readonly IContainer _container = new ContainerBuilder()
.WithImage(CommonImages.Alpine)
.WithEntrypoint("/bin/sh", "-c")
.WithCommand($"while true; do echo \"HTTP/1.1 200 OK\r\n\r\n\" | nc -l -p {HttpPort}; done")
.WithImage(CommonImages.Socat)
.WithCommand("-v")
.WithCommand($"TCP-LISTEN:{HttpPort},crlf,reuseaddr,fork")
.WithCommand("EXEC:\"echo -e 'HTTP/1.1 200 OK'\n\n\"")
.WithPortBinding(HttpPort, true)
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request => request))
.Build();

public static TheoryData<HttpWaitStrategy> GetHttpWaitStrategies()
Expand Down Expand Up @@ -74,15 +76,15 @@ public async Task HttpWaitStrategySendsHeaders()
await Task.Delay(TimeSpan.FromSeconds(1))
.ConfigureAwait(true);

var (stdout, _) = await _container.GetLogsAsync()
var (_, stderr) = await _container.GetLogsAsync()
.ConfigureAwait(true);

// Then
Assert.True(succeeded);
Assert.Contains("Authorization", stdout);
Assert.Contains("QWxhZGRpbjpvcGVuIHNlc2FtZQ==", stdout);
Assert.Contains(httpHeaders.First().Key, stdout);
Assert.Contains(httpHeaders.First().Value, stdout);
Assert.Contains("Authorization", stderr);
Assert.Contains("QWxhZGRpbjpvcGVuIHNlc2FtZQ==", stderr);
Assert.Contains(httpHeaders.First().Key, stderr);
Assert.Contains(httpHeaders.First().Value, stderr);
}

[Fact]
Expand All @@ -104,13 +106,13 @@ public async Task HttpWaitStrategyUsesCustomHttpClientHandler()
await Task.Delay(TimeSpan.FromSeconds(1))
.ConfigureAwait(true);

var (stdout, _) = await _container.GetLogsAsync()
var (_, stderr) = await _container.GetLogsAsync()
.ConfigureAwait(true);

// Then
Assert.True(succeeded);
Assert.Contains("Cookie", stdout);
Assert.Contains("Key1=Value1", stdout);
Assert.Contains("Cookie", stderr);
Assert.Contains("Key1=Value1", stderr);
}

[Fact]
Expand Down