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

Update the .NET Aspire tests #1170

Merged
merged 2 commits into from
Sep 10, 2024
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
5 changes: 1 addition & 4 deletions test/WireMock.Net.Aspire.Tests/DockerUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ namespace WireMock.Net.Aspire.Tests;
[ExcludeFromCodeCoverage]
internal static class DockerUtils
{
public static bool IsDockerRunningLinuxContainerMode()
{
return IsDockerRunning() && IsLinuxContainerMode();
}
public static Lazy<bool> IsDockerRunningLinuxContainerMode => new(() => IsDockerRunning() && IsLinuxContainerMode());

private static bool IsDockerRunning()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright © WireMock.Net

namespace WireMock.Net.Aspire.Tests.Facts;

public sealed class DockerIsRunningInLinuxContainerModeFact : FactAttribute
{
private const string SkipReason = "Docker is not running in Linux container mode. Skipping test.";

public DockerIsRunningInLinuxContainerModeFact()
{
if (!DockerUtils.IsDockerRunningLinuxContainerMode.Value)
{
Skip = SkipReason;
}

Check warning on line 14 in test/WireMock.Net.Aspire.Tests/Facts/DockerIsRunningInLinuxContainerModeFact.cs

View check run for this annotation

Codecov / codecov/patch

test/WireMock.Net.Aspire.Tests/Facts/DockerIsRunningInLinuxContainerModeFact.cs#L12-L14

Added lines #L12 - L14 were not covered by tests
}
}
17 changes: 3 additions & 14 deletions test/WireMock.Net.Aspire.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net.Http.Json;
using FluentAssertions;
using Projects;
using WireMock.Net.Aspire.Tests.Facts;
using Xunit.Abstractions;

namespace WireMock.Net.Aspire.Tests;
Expand All @@ -11,15 +12,9 @@ public class IntegrationTests(ITestOutputHelper output)
{
private record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary);

[Fact]
[DockerIsRunningInLinuxContainerModeFact]
public async Task StartAppHostWithWireMockAndCreateHttpClientToCallTheMockedWeatherForecastEndpoint()
{
if (!DockerUtils.IsDockerRunningLinuxContainerMode())
{
output.WriteLine("Docker is not running in Linux container mode. Skipping test.");
return;
}

// Arrange
var appHostBuilder = await DistributedApplicationTestingBuilder.CreateAsync<WireMock_Net_Aspire_TestAppHost>();
await using var app = await appHostBuilder.BuildAsync();
Expand All @@ -44,15 +39,9 @@ public async Task StartAppHostWithWireMockAndCreateHttpClientToCallTheMockedWeat
weatherForecasts2.Should().HaveCount(5);
}

[Fact]
[DockerIsRunningInLinuxContainerModeFact]
public async Task StartAppHostWithWireMockAndCreateWireMockAdminClientToCallTheAdminEndpoint()
{
if (!DockerUtils.IsDockerRunningLinuxContainerMode())
{
output.WriteLine("Docker is not running in Linux container mode. Skipping test.");
return;
}

// Arrange
var appHostBuilder = await DistributedApplicationTestingBuilder.CreateAsync<WireMock_Net_Aspire_TestAppHost>();
await using var app = await appHostBuilder.BuildAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ namespace WireMock.Net.Tests.Facts;

public sealed class IgnoreOnContinuousIntegrationFact : FactAttribute
{
private static readonly string _skipReason = "Ignore when run via CI/CD";
private static readonly bool _isContinuousIntegrationAzure = bool.TryParse(Environment.GetEnvironmentVariable("TF_BUILD"), out var isTF) && isTF;
private static readonly bool _isContinuousIntegrationGithub = bool.TryParse(Environment.GetEnvironmentVariable("GITHUB_ACTIONS"), out var isGH) && isGH;
private static bool IsContinuousIntegration() => _isContinuousIntegrationAzure || _isContinuousIntegrationGithub;
private const string SkipReason = "Ignore when run via CI/CD";
private static readonly bool IsContinuousIntegrationAzure = bool.TryParse(Environment.GetEnvironmentVariable("TF_BUILD"), out var isTF) && isTF;
private static readonly bool IsContinuousIntegrationGithub = bool.TryParse(Environment.GetEnvironmentVariable("GITHUB_ACTIONS"), out var isGH) && isGH;
private static readonly bool IsContinuousIntegration = IsContinuousIntegrationAzure || IsContinuousIntegrationGithub;

public IgnoreOnContinuousIntegrationFact()
{
if (IsContinuousIntegration())
if (IsContinuousIntegration)
{
Skip = _skipReason;
Skip = SkipReason;
}
}
}
}
38 changes: 17 additions & 21 deletions test/WireMock.Net.Tests/WireMockServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
{
_testOutputHelper = testOutputHelper;
}

[Fact]
public void WireMockServer_Start()
{
Expand Down Expand Up @@ -209,7 +209,7 @@
.Select(addr => addr.Address.ToString())
.ToArray();
}

[IgnoreOnContinuousIntegrationFact]
public async Task WireMockServer_WithUrl0000_Should_Listen_On_All_IPs_IPv4()
{
Expand All @@ -218,22 +218,20 @@
var IPv4 = GetIPAddressesByFamily(System.Net.Sockets.AddressFamily.InterNetwork);
var settings = new WireMockServerSettings
{
Urls = new string[] { "http://0.0.0.0:" + port },
Urls = ["http://0.0.0.0:" + port],

Check warning on line 221 in test/WireMock.Net.Tests/WireMockServerTests.cs

View check run for this annotation

Codecov / codecov/patch

test/WireMock.Net.Tests/WireMockServerTests.cs#L221

Added line #L221 was not covered by tests
};
var server = WireMockServer.Start(settings);
using var server = WireMockServer.Start(settings);

Check warning on line 223 in test/WireMock.Net.Tests/WireMockServerTests.cs

View check run for this annotation

Codecov / codecov/patch

test/WireMock.Net.Tests/WireMockServerTests.cs#L223

Added line #L223 was not covered by tests

server.Given(Request.Create().WithPath("/*")).RespondWith(Response.Create().WithBody("x"));
foreach (var addr in IPv4)

foreach (var address in IPv4)

Check warning on line 227 in test/WireMock.Net.Tests/WireMockServerTests.cs

View check run for this annotation

Codecov / codecov/patch

test/WireMock.Net.Tests/WireMockServerTests.cs#L227

Added line #L227 was not covered by tests
{
// Act
var response = await new HttpClient().GetStringAsync("http://" + addr + ":" + server.Ports[0] + "/foo").ConfigureAwait(false);
var response = await new HttpClient().GetStringAsync("http://" + address + ":" + server.Ports[0] + "/foo").ConfigureAwait(false);

Check warning on line 230 in test/WireMock.Net.Tests/WireMockServerTests.cs

View check run for this annotation

Codecov / codecov/patch

test/WireMock.Net.Tests/WireMockServerTests.cs#L230

Added line #L230 was not covered by tests

// Assert
response.Should().Be("x");
}

server.Stop();
}

Check warning on line 234 in test/WireMock.Net.Tests/WireMockServerTests.cs

View check run for this annotation

Codecov / codecov/patch

test/WireMock.Net.Tests/WireMockServerTests.cs#L234

Added line #L234 was not covered by tests
}

[IgnoreOnContinuousIntegrationFact]
Expand All @@ -244,25 +242,23 @@
var IPv6 = GetIPAddressesByFamily(System.Net.Sockets.AddressFamily.InterNetworkV6);
var settings = new WireMockServerSettings
{
Urls = new string[] { "http://0.0.0.0:" + port },
Urls = ["http://0.0.0.0:" + port],

Check warning on line 245 in test/WireMock.Net.Tests/WireMockServerTests.cs

View check run for this annotation

Codecov / codecov/patch

test/WireMock.Net.Tests/WireMockServerTests.cs#L245

Added line #L245 was not covered by tests
};
var server = WireMockServer.Start(settings);
using var server = WireMockServer.Start(settings);

Check warning on line 247 in test/WireMock.Net.Tests/WireMockServerTests.cs

View check run for this annotation

Codecov / codecov/patch

test/WireMock.Net.Tests/WireMockServerTests.cs#L247

Added line #L247 was not covered by tests

server.Given(Request.Create().WithPath("/*")).RespondWith(Response.Create().WithBody("x"));

foreach (var addr in IPv6)
foreach (var address in IPv6)

Check warning on line 251 in test/WireMock.Net.Tests/WireMockServerTests.cs

View check run for this annotation

Codecov / codecov/patch

test/WireMock.Net.Tests/WireMockServerTests.cs#L251

Added line #L251 was not covered by tests
{
// Act
var response = await new HttpClient().GetStringAsync("http://[" + addr + "]:" + server.Ports[0] + "/foo").ConfigureAwait(false);
var response = await new HttpClient().GetStringAsync("http://[" + address + "]:" + server.Ports[0] + "/foo").ConfigureAwait(false);

Check warning on line 254 in test/WireMock.Net.Tests/WireMockServerTests.cs

View check run for this annotation

Codecov / codecov/patch

test/WireMock.Net.Tests/WireMockServerTests.cs#L254

Added line #L254 was not covered by tests

// Assert
response.Should().Be("x");
}

server.Stop();
}
#endif

[Fact]
public async Task WireMockServer_Should_respond_a_redirect_without_body()
{
Expand Down