Skip to content

Commit

Permalink
Migrate HealthChecks.SqlServer tests to Testcontainers
Browse files Browse the repository at this point in the history
  • Loading branch information
Alirexaa committed Dec 17, 2024
1 parent f54852f commit 72b1578
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
<PackageVersion Include="System.Text.RegularExpressions" Version="4.3.1" />
<PackageVersion Include="System.Threading.Channels" Version="8.0.0" />
<PackageVersion Include="Testcontainers.MsSql" Version="$(TestcontainersVersion)" />
<PackageVersion Include="Testcontainers.Redis" Version="$(TestcontainersVersion)" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace HealthChecks.SqlServer.Tests.Functional;

public class sqlserver_healthcheck_should
public class sqlserver_healthcheck_should(SqlServerContainerFixture sqlServerContainerFixture) : IClassFixture<SqlServerContainerFixture>
{
[Fact]
public async Task be_healthy_if_sqlServer_is_available()
{
var connectionString = "Server=tcp:localhost,5433;Initial Catalog=master;User Id=sa;Password=Password12!;Encrypt=false";
var connectionString = sqlServerContainerFixture.GetConnectionString();

var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
Expand Down Expand Up @@ -57,7 +57,7 @@ public async Task be_unhealthy_if_sqlServer_is_not_available()
[Fact]
public async Task be_unhealthy_if_sqlquery_spec_is_not_valid()
{
var connectionString = "Server=tcp:localhost,5433;Initial Catalog=master;User Id=sa;Password=Password12!;Encrypt=false";
var connectionString = sqlServerContainerFixture.GetConnectionString();

var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="Testcontainers.MsSql" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\HealthChecks.SqlServer\HealthChecks.SqlServer.csproj" />
</ItemGroup>
Expand Down
35 changes: 35 additions & 0 deletions test/HealthChecks.SqlServer.Tests/RedisContainerFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Testcontainers.MsSql;

namespace HealthChecks.SqlServer.Tests;

public sealed class SqlServerContainerFixture : IAsyncLifetime
{
public const string Registry = "mcr.microsoft.com";

public const string Image = "mssql/server";

public const string Tag = "2022-latest";

public MsSqlContainer? Container { get; private set; }

public string GetConnectionString() => Container?.GetConnectionString() ??
throw new InvalidOperationException("The test container was not initialized.");

public async Task InitializeAsync() => Container = await CreateContainerAsync();

public async Task DisposeAsync()
{
if (Container is not null)
await Container.DisposeAsync();
}

public static async Task<MsSqlContainer> CreateContainerAsync()
{
var container = new MsSqlBuilder()
.WithImage($"{Registry}/{Image}:{Tag}")
.Build();
await container.StartAsync();

return container;
}
}

0 comments on commit 72b1578

Please sign in to comment.