Skip to content

Commit 5d4282a

Browse files
authored
Migrate HealthChecks.SurrealDb tests to Testcontainers (#2402)
1 parent a3d9009 commit 5d4282a

File tree

4 files changed

+75
-47
lines changed

4 files changed

+75
-47
lines changed

.github/workflows/healthchecks_surrealdb_ci.yml

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,47 +29,8 @@ on:
2929

3030
jobs:
3131
build:
32-
runs-on: ubuntu-latest
33-
steps:
34-
- uses: actions/checkout@v3
35-
- name: Start SurrealDB
36-
uses: surrealdb/setup-surreal@v2
37-
with:
38-
surrealdb_version: latest
39-
surrealdb_port: 8000
40-
surrealdb_username: root
41-
surrealdb_password: root
42-
surrealdb_auth: true
43-
- name: Setup .NET
44-
uses: actions/setup-dotnet@v4
45-
with:
46-
dotnet-version: |
47-
8.0.x
48-
9.0.x
49-
- name: Restore
50-
run: |
51-
dotnet restore ./src/HealthChecks.SurrealDb/HealthChecks.SurrealDb.csproj &&
52-
dotnet restore ./test/HealthChecks.SurrealDb.Tests/HealthChecks.SurrealDb.Tests.csproj
53-
- name: Check formatting
54-
run: |
55-
dotnet format --no-restore --verify-no-changes --severity warn ./src/HealthChecks.SurrealDb/HealthChecks.SurrealDb.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1) &&
56-
dotnet format --no-restore --verify-no-changes --severity warn ./test/HealthChecks.SurrealDb.Tests/HealthChecks.SurrealDb.Tests.csproj || (echo "Run 'dotnet format' to fix issues" && exit 1)
57-
- name: Build
58-
run: |
59-
dotnet build --no-restore ./src/HealthChecks.SurrealDb/HealthChecks.SurrealDb.csproj &&
60-
dotnet build --no-restore ./test/HealthChecks.SurrealDb.Tests/HealthChecks.SurrealDb.Tests.csproj
61-
- name: Test
62-
run: >
63-
dotnet test
64-
./test/HealthChecks.SurrealDb.Tests/HealthChecks.SurrealDb.Tests.csproj
65-
--no-restore
66-
--no-build
67-
--collect "XPlat Code Coverage"
68-
--results-directory .coverage
69-
--
70-
DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover
71-
- name: Upload Coverage
72-
uses: codecov/codecov-action@v5
73-
with:
74-
flags: SurrealDb
75-
directory: .coverage
32+
uses: ./.github/workflows/reusable_ci_workflow.yml
33+
with:
34+
PROJECT_PATH: ./src/HealthChecks.SurrealDb/HealthChecks.SurrealDb.csproj
35+
TEST_PROJECT_PATH: ./test/HealthChecks.SurrealDb.Tests/HealthChecks.SurrealDb.Tests.csproj
36+
CODECOV_FLAGS: SurrealDb

test/HealthChecks.SurrealDb.Tests/Functional/SurrealDbHealthCheckTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace HealthChecks.SurrealDb.Tests.Functional;
44

5-
public class surrealdb_healthcheck_should
5+
public class surrealdb_healthcheck_should(SurrealDbContainerFixture surrealDbFixture) : IClassFixture<SurrealDbContainerFixture>
66
{
77
[Fact]
88
public async Task be_healthy_if_surrealdb_is_available()
99
{
10-
const string connectionString = "Server=http://localhost:8000;Namespace=test;Database=test;Username=root;Password=root";
10+
string connectionString = surrealDbFixture.GetConnectionString();
1111

1212
var webHostBuilder = new WebHostBuilder()
1313
.ConfigureServices(services =>
@@ -36,7 +36,7 @@ public async Task be_healthy_if_surrealdb_is_available()
3636
[Fact]
3737
public async Task be_unhealthy_if_surrealdb_is_not_available()
3838
{
39-
const string connectionString = "Server=http://localhost:1234;Namespace=test;Database=test;Username=root;Password=root";
39+
const string connectionString = "Server=http://localhost:1234;Username=root;Password=root";
4040

4141
var webHostBuilder = new WebHostBuilder()
4242
.ConfigureServices(services =>

test/HealthChecks.SurrealDb.Tests/HealthChecks.SurrealDb.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44
<ProjectReference Include="..\..\src\HealthChecks.SurrealDb\HealthChecks.SurrealDb.csproj" />
55
</ItemGroup>
66

7+
<ItemGroup>
8+
<PackageReference Include="Testcontainers" />
9+
</ItemGroup>
10+
711
</Project>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.Data.Common;
2+
using DotNet.Testcontainers.Builders;
3+
using DotNet.Testcontainers.Containers;
4+
5+
namespace HealthChecks.SurrealDb.Tests;
6+
7+
public class SurrealDbContainerFixture : IAsyncLifetime
8+
{
9+
private const string Registry = "docker.io";
10+
11+
private const string Image = "surrealdb/surrealdb";
12+
13+
private const string Tag = "v2.3.7";
14+
15+
private const int Port = 8000;
16+
17+
private const string Username = "root";
18+
19+
private const string Password = "root";
20+
21+
public IContainer? Container { get; private set; }
22+
23+
public string GetConnectionString()
24+
{
25+
if (Container is null)
26+
{
27+
throw new InvalidOperationException("The test container was not initialized.");
28+
}
29+
30+
string endpoint = new UriBuilder("http", Container.Hostname, Container.GetMappedPublicPort(Port)).ToString();
31+
32+
var builder = new DbConnectionStringBuilder
33+
{
34+
{ "Server", endpoint },
35+
{ "Username", Username },
36+
{ "Password", Password }
37+
};
38+
39+
return builder.ConnectionString;
40+
}
41+
42+
public async Task InitializeAsync() => Container = await CreateContainerAsync();
43+
44+
public Task DisposeAsync() => Container?.DisposeAsync().AsTask() ?? Task.CompletedTask;
45+
46+
private static async Task<IContainer> CreateContainerAsync()
47+
{
48+
var waitStrategy = Wait
49+
.ForUnixContainer()
50+
.UntilHttpRequestIsSucceeded(x => x.ForPath("/health").ForPort(Port));
51+
52+
var container = new ContainerBuilder()
53+
.WithImage($"{Registry}/{Image}:{Tag}")
54+
.WithPortBinding(Port, true)
55+
.WithCommand("start", "--user", Username, "--pass", Password, "memory")
56+
.WithWaitStrategy(waitStrategy)
57+
.Build();
58+
59+
await container.StartAsync();
60+
61+
return container;
62+
}
63+
}

0 commit comments

Comments
 (0)