Skip to content

Commit

Permalink
v0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
KSemenenko committed Sep 9, 2024
1 parent de0cea3 commit 183f56d
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
<RepositoryUrl>https://github.com/managedcode/IntegrationTestBaseKit</RepositoryUrl>
<PackageProjectUrl>https://github.com/managedcode/IntegrationTestBaseKit</PackageProjectUrl>
<Product>Managed Code - IntegrationTestBaseKit</Product>
<Version>0.0.1</Version>
<PackageVersion>0.0.1</PackageVersion>
<Version>0.0.2</Version>
<PackageVersion>0.0.2</PackageVersion>

</PropertyGroup>
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
Expand Down
17 changes: 17 additions & 0 deletions ManagedCode.IntegrationTestBaseKit.Tests/HealthTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using FluentAssertions;
using TestBlazorApp;
using Testcontainers.Azurite;
using Testcontainers.PostgreSql;
using Xunit.Abstractions;

namespace ManagedCode.IntegrationTestBaseKit.Tests;
Expand All @@ -23,4 +26,18 @@ public async Task BrowserHealthTest()
.Should()
.BeTrue();
}

[Fact]
public void ConnectionStringTest()
{
StaticContainer.AzureBlobConnectionString
.Should()
.Be(testApplication.GetContainer<AzuriteContainer>()
.GetConnectionString());

StaticContainer.PostgreSqlConnectionString
.Should()
.Be(testApplication.GetContainer<PostgreSqlContainer>("postgree")
.GetConnectionString());
}
}
8 changes: 8 additions & 0 deletions ManagedCode.IntegrationTestBaseKit.Tests/TestApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@ protected override async Task ConfigureTestContainers()
AddContainer(new AzuriteBuilder().Build());
AddContainer("postgree", new PostgreSqlBuilder().Build());
}

protected override void ConfigureConfiguration()
{
SetConfigurationValue("AzureBlob", GetContainer<AzuriteContainer>()
.GetConnectionString());
SetConfigurationValue("ConnectionStrings:PostgreSql", GetContainer<PostgreSqlContainer>("postgree")
.GetConnectionString());
}
}
11 changes: 8 additions & 3 deletions ManagedCode.IntegrationTestBaseKit.XUnit/BaseXUnitTestApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

namespace ManagedCode.IntegrationTestBaseKit.XUnit;

public abstract class BaseXUnitTestApp<TEntryPoint> : BaseTestApp<TEntryPoint>, IAsyncLifetime
where TEntryPoint : class
public abstract class BaseXUnitTestApp<TEntryPoint> : BaseTestApp<TEntryPoint>, IAsyncLifetime where TEntryPoint : class
{
async Task IAsyncLifetime.InitializeAsync()
{
await InitializeAsync();
CreateHttpClient();
}

async Task IAsyncLifetime.DisposeAsync()
{
await base.DisposeAsync();
await DisposeAsync();
}
}
52 changes: 43 additions & 9 deletions ManagedCode.IntegrationTestBaseKit/BaseTestApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
using Microsoft.AspNetCore.Http.Connections.Client;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Playwright;

namespace ManagedCode.IntegrationTestBaseKit;

public abstract class BaseTestApp<TEntryPoint> : WebApplicationFactory<TEntryPoint>
where TEntryPoint : class
public abstract class BaseTestApp<TEntryPoint> : WebApplicationFactory<TEntryPoint> where TEntryPoint : class
{
private IHost? _host;

private readonly ConfigurationBuilder ConfigurationBuilder = new();

protected virtual bool UsePlaywright { get; } = true;
private PlaywrightWrapper Fixture { get; } = new();

protected Dictionary<string, DockerContainer> Containers { get; } = new();
Expand Down Expand Up @@ -62,18 +65,44 @@ public T GetContainer<T>() where T : DockerContainer
public virtual async Task InitializeAsync()
{
await ConfigureTestContainers();
await Fixture.InitializeAsync();

if (UsePlaywright)
await Fixture.InitializeAsync();

foreach (var container in Containers)
await container.Value.StartAsync();
}

protected override IHost CreateHost(IHostBuilder builder)
{
ConfigureConfiguration();
var configuration = ConfigurationBuilder.Build();
builder.ConfigureWebHost(hostBuilder =>
{
foreach (var setting in configuration.AsEnumerable(true))
hostBuilder.UseSetting(setting.Key, setting.Value);
});

// Create the host for TestServer now before we
// modify the builder to use Kestrel instead.
var testHost = builder.Build();

// Modify the host builder to use Kestrel instead
// of TestServer so we can listen on a real address.
builder.ConfigureWebHost(hostBuilder => hostBuilder.UseKestrel());
_host = builder.Build();

// Create and start the Kestrel server before the test server,
// otherwise due to the way the deferred host builder works
// for minimal hosting, the server will not get "initialized
// enough" for the address it is listening on to be available.
// See https://github.com/dotnet/aspnetcore/issues/33846.
_host = builder.Build(); //base.CreateHost(builder);
_host.Start();

// Extract the selected dynamic port out of the Kestrel server
// and assign it onto the client options for convenience so it
// "just works" as otherwise it'll be the default http://localhost
// URL, which won't route to the Kestrel-hosted HTTP server.
var server = _host.Services.GetRequiredService<IServer>();
var addressFeature = server.Features.Get<IServerAddressesFeature>();
ClientOptions.BaseAddress = addressFeature!.Addresses
Expand All @@ -84,13 +113,9 @@ protected override IHost CreateHost(IHostBuilder builder)
return testHost;
}

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Development");
}

public override async ValueTask DisposeAsync()
{
_host?.Dispose();
await Fixture.DisposeAsync();
foreach (var container in Containers)
{
Expand Down Expand Up @@ -125,6 +150,9 @@ public HubConnection CreateSignalRClient(string hubUrl, Action<HubConnectionBuil

public async Task<IPage> OpenNewPage(string url)
{
if (!UsePlaywright)
throw new InvalidOperationException("Playwright is not enabled");

var fullUrl = new Uri(ServerUri, url).ToString();
var context = await Browser.NewContextAsync();
var page = await context.NewPageAsync();
Expand All @@ -144,4 +172,10 @@ protected void AddContainer(DockerContainer container)
}

protected abstract Task ConfigureTestContainers();
protected abstract void ConfigureConfiguration();

protected void SetConfigurationValue(string key, string value)
{
ConfigurationBuilder.AddInMemoryCollection(new Dictionary<string, string> { { key, value } }!);
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dotnet add package ManagedCode.IntegrationTestBaseKit
```

for xUnit integration use the following command:

```sh
dotnet add package ManagedCode.ManagedCode.IntegrationTestBaseKit.XUnit
```
Expand Down
5 changes: 5 additions & 0 deletions TestBlazorApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public static void Main(string[] args)
.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);


StaticContainer.PostgreSqlConnectionString = builder.Configuration.GetConnectionString("PostgreSql");

Check warning on line 25 in TestBlazorApp/Program.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference assignment.

Check warning on line 25 in TestBlazorApp/Program.cs

View workflow job for this annotation

GitHub Actions / nuget-pack

Possible null reference assignment.
StaticContainer.AzureBlobConnectionString = builder.Configuration.GetValue<string>("AzureBlob");

Check warning on line 26 in TestBlazorApp/Program.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Possible null reference assignment.

Check warning on line 26 in TestBlazorApp/Program.cs

View workflow job for this annotation

GitHub Actions / nuget-pack

Possible null reference assignment.


var app = builder.Build();

// Configure the HTTP request pipeline.
Expand Down
7 changes: 7 additions & 0 deletions TestBlazorApp/StaticContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TestBlazorApp;

public static class StaticContainer
{
public static string PostgreSqlConnectionString;

Check warning on line 5 in TestBlazorApp/StaticContainer.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Non-nullable field 'PostgreSqlConnectionString' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 5 in TestBlazorApp/StaticContainer.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Non-nullable field 'PostgreSqlConnectionString' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 5 in TestBlazorApp/StaticContainer.cs

View workflow job for this annotation

GitHub Actions / nuget-pack

Non-nullable field 'PostgreSqlConnectionString' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 5 in TestBlazorApp/StaticContainer.cs

View workflow job for this annotation

GitHub Actions / nuget-pack

Non-nullable field 'PostgreSqlConnectionString' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
public static string AzureBlobConnectionString;

Check warning on line 6 in TestBlazorApp/StaticContainer.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Non-nullable field 'AzureBlobConnectionString' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 6 in TestBlazorApp/StaticContainer.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Non-nullable field 'AzureBlobConnectionString' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 6 in TestBlazorApp/StaticContainer.cs

View workflow job for this annotation

GitHub Actions / nuget-pack

Non-nullable field 'AzureBlobConnectionString' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 6 in TestBlazorApp/StaticContainer.cs

View workflow job for this annotation

GitHub Actions / nuget-pack

Non-nullable field 'AzureBlobConnectionString' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
}

0 comments on commit 183f56d

Please sign in to comment.