-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Aspire * Got aspire running * Added Aspire service defaults * Remove docker compose and up.ps1. * Got integration tests working * Remove unneeded code * Remove incorrect connection string * Fixed up connection string for tests * Fix merge conflicts
- Loading branch information
1 parent
5a3243c
commit 63e90fa
Showing
17 changed files
with
306 additions
and
136 deletions.
There are no files selected for viewing
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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 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 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,118 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Diagnostics.HealthChecks; | ||
using Microsoft.Extensions.Logging; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Metrics; | ||
using OpenTelemetry.Trace; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.Extensions.Hosting; | ||
|
||
// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry. | ||
// This project should be referenced by each service project in your solution. | ||
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults | ||
public static class Extensions | ||
{ | ||
public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder | ||
{ | ||
builder.ConfigureOpenTelemetry(); | ||
|
||
builder.AddDefaultHealthChecks(); | ||
|
||
builder.Services.AddServiceDiscovery(); | ||
|
||
builder.Services.ConfigureHttpClientDefaults(http => | ||
{ | ||
// Turn on resilience by default | ||
http.AddStandardResilienceHandler(); | ||
|
||
// Turn on service discovery by default | ||
http.AddServiceDiscovery(); | ||
}); | ||
|
||
// Uncomment the following to restrict the allowed schemes for service discovery. | ||
// builder.Services.Configure<ServiceDiscoveryOptions>(options => | ||
// { | ||
// options.AllowedSchemes = ["https"]; | ||
// }); | ||
|
||
return builder; | ||
} | ||
|
||
private static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) | ||
where TBuilder : IHostApplicationBuilder | ||
{ | ||
builder.Logging.AddOpenTelemetry(logging => | ||
{ | ||
logging.IncludeFormattedMessage = true; | ||
logging.IncludeScopes = true; | ||
}); | ||
|
||
builder.Services.AddOpenTelemetry() | ||
.WithMetrics(metrics => | ||
{ | ||
metrics.AddAspNetCoreInstrumentation() | ||
.AddHttpClientInstrumentation() | ||
.AddRuntimeInstrumentation(); | ||
}) | ||
.WithTracing(tracing => | ||
{ | ||
tracing.AddAspNetCoreInstrumentation() | ||
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package) | ||
//.AddGrpcClientInstrumentation() | ||
.AddHttpClientInstrumentation(); | ||
}); | ||
|
||
builder.AddOpenTelemetryExporters(); | ||
|
||
return builder; | ||
} | ||
|
||
private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder) | ||
where TBuilder : IHostApplicationBuilder | ||
{ | ||
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); | ||
|
||
if (useOtlpExporter) | ||
{ | ||
builder.Services.AddOpenTelemetry().UseOtlpExporter(); | ||
} | ||
|
||
// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package) | ||
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"])) | ||
//{ | ||
// builder.Services.AddOpenTelemetry() | ||
// .UseAzureMonitor(); | ||
//} | ||
|
||
return builder; | ||
} | ||
|
||
private static TBuilder AddDefaultHealthChecks<TBuilder>(this TBuilder builder) | ||
where TBuilder : IHostApplicationBuilder | ||
{ | ||
builder.Services.AddHealthChecks() | ||
// Add a default liveness check to ensure app is responsive | ||
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); | ||
|
||
return builder; | ||
} | ||
|
||
public static WebApplication MapDefaultEndpoints(this WebApplication app) | ||
{ | ||
// Adding health checks endpoints to applications in non-development environments has security implications. | ||
// See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
// All health checks must pass for app to be considered ready to accept traffic after starting | ||
app.MapHealthChecks("/health"); | ||
|
||
// Only health checks tagged with the "live" tag must pass for app to be considered alive | ||
app.MapHealthChecks("/alive", new HealthCheckOptions { Predicate = r => r.Tags.Contains("live") }); | ||
} | ||
|
||
return app; | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,23 +1,24 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageVersion Include="Azure.Identity" Version="1.13.0" /> | ||
<PackageVersion Include="FluentAssertions" Version="6.12.0" /> | ||
<PackageVersion Include="JunitXml.TestLogger" Version="3.1.12" /> | ||
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" /> | ||
<PackageVersion Include="xunit" Version="2.7.0" /> | ||
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.8" /> | ||
<PackageVersion Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageVersion Include="NetArchTest.Rules" Version="1.3.2" /> | ||
<PackageVersion Include="Bogus" Version="34.0.2" /> | ||
<PackageVersion Include="NSubstitute" Version="5.1.0" /> | ||
<PackageVersion Include="Respawn" Version="6.2.1" /> | ||
<PackageVersion Include="Testcontainers" Version="3.8.0" /> | ||
<PackageVersion Include="Testcontainers.SqlEdge" Version="3.8.0" /> | ||
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.0-rc.2.24474.3" /> | ||
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0-rc.2.24473.5" /> | ||
<PackageVersion Include="Meziantou.Extensions.Logging.Xunit" Version="1.0.7" /> | ||
</ItemGroup> | ||
</Project> | ||
<PropertyGroup> | ||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageVersion Include="Azure.Identity" Version="1.13.0"/> | ||
<PackageVersion Include="FluentAssertions" Version="6.12.0"/> | ||
<PackageVersion Include="JunitXml.TestLogger" Version="3.1.12"/> | ||
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1"/> | ||
<PackageVersion Include="xunit" Version="2.7.0"/> | ||
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.8"/> | ||
<PackageVersion Include="coverlet.collector" Version="6.0.0"/> | ||
<PackageVersion Include="NetArchTest.Rules" Version="1.3.2"/> | ||
<PackageVersion Include="Bogus" Version="34.0.2"/> | ||
<PackageVersion Include="NSubstitute" Version="5.1.0"/> | ||
<PackageVersion Include="Respawn" Version="6.2.1"/> | ||
<PackageVersion Include="Testcontainers" Version="3.10.0"/> | ||
<PackageVersion Include="Testcontainers.MsSql" Version="3.10.0"/> | ||
<PackageVersion Include="Polly" Version="8.4.2"/> | ||
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.0-rc.2.24474.3"/> | ||
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0-rc.2.24473.5"/> | ||
<PackageVersion Include="Meziantou.Extensions.Logging.Xunit" Version="1.0.7"/> | ||
</ItemGroup> | ||
</Project> |
38 changes: 27 additions & 11 deletions
38
tests/WebApi.IntegrationTests/Common/Fixtures/DatabaseContainer.cs
This file contains 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 |
---|---|---|
@@ -1,28 +1,44 @@ | ||
using Testcontainers.SqlEdge; | ||
using Polly; | ||
using Testcontainers.MsSql; | ||
|
||
namespace WebApi.IntegrationTests.Common.Fixtures; | ||
|
||
/// <summary> | ||
/// Wraper for SQL edge container | ||
/// </summary> | ||
public class DatabaseContainer | ||
public class DatabaseContainer : IAsyncDisposable | ||
{ | ||
private readonly SqlEdgeContainer? _container = new SqlEdgeBuilder() | ||
.WithName("CleanArchitecture-IntegrationTests-DbContainer") | ||
.WithPassword("sqledge!Strong") | ||
private readonly MsSqlContainer _container = new MsSqlBuilder() | ||
.WithImage("mcr.microsoft.com/mssql/server:2022-CU14-ubuntu-22.04") | ||
.WithName($"CleanArchitecture-IntegrationTests-{Guid.NewGuid()}") | ||
.WithPassword("Password123") | ||
.WithPortBinding(1433, true) | ||
.WithAutoRemove(true) | ||
.Build(); | ||
|
||
private const int MaxRetries = 5; | ||
|
||
public string? ConnectionString { get; private set; } | ||
|
||
public async Task InitializeAsync() | ||
{ | ||
if (_container != null) | ||
{ | ||
await _container.StartAsync(); | ||
ConnectionString = _container.GetConnectionString(); | ||
} | ||
await StartWithRetry(); | ||
ConnectionString = _container.GetConnectionString(); | ||
} | ||
|
||
public Task DisposeAsync() => _container?.StopAsync() ?? Task.CompletedTask; | ||
private async Task StartWithRetry() | ||
{ | ||
// NOTE: For some reason the container sometimes fails to start up. Add in a retry to protect against this | ||
var policy = Policy.Handle<InvalidOperationException>() | ||
.WaitAndRetryAsync(MaxRetries, _ => TimeSpan.FromSeconds(5)); | ||
|
||
await policy.ExecuteAsync(async () => { await _container.StartAsync(); }); | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
await _container.StopAsync(); | ||
await _container.DisposeAsync(); | ||
GC.SuppressFinalize(this); | ||
} | ||
} |
Oops, something went wrong.