Skip to content

Commit 682ee60

Browse files
committed
Migrate HealthChecks.Oracle tests to Testcontainers
1 parent 846fbc9 commit 682ee60

File tree

5 files changed

+65
-53
lines changed

5 files changed

+65
-53
lines changed

.github/workflows/healthchecks_oracle_ci.yml

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

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

Directory.Packages.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
<PackageVersion Include="Testcontainers.ClickHouse" Version="$(TestcontainersVersion)" />
107107
<PackageVersion Include="Testcontainers.Kafka" Version="$(TestcontainersVersion)" />
108108
<PackageVersion Include="Testcontainers.Milvus" Version="$(TestcontainersVersion)" />
109+
<PackageVersion Include="Testcontainers.Oracle" Version="$(TestcontainersVersion)" />
109110
<PackageVersion Include="Testcontainers.PostgreSql" Version="$(TestcontainersVersion)" />
110111
<PackageVersion Include="TestContainers.MongoDb" Version="$(TestcontainersVersion)" />
111112
<PackageVersion Include="Testcontainers.MsSql" Version="$(TestcontainersVersion)" />
@@ -119,4 +120,4 @@
119120
<ItemGroup Condition="'$(IsPackable)' == 'true'">
120121
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
121122
</ItemGroup>
122-
</Project>
123+
</Project>

test/HealthChecks.Oracle.Tests/Functional/OracleHealthCheckTests.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace HealthChecks.Oracle.Tests.Functional;
66

7-
public class oracle_healthcheck_should
7+
public class oracle_healthcheck_should(OracleContainerFixture oracleFixture) : IClassFixture<OracleContainerFixture>
88
{
99
[Fact]
1010
public async Task be_healthy_when_oracle_is_available()
1111
{
12-
var connectionString = "Data Source=localhost:1521/XEPDB1;User Id=system;Password=oracle";
12+
string connectionString = oracleFixture.GetConnectionString();
1313

1414
var webHostBuilder = new WebHostBuilder()
1515
.ConfigureServices(services =>
@@ -34,13 +34,15 @@ public async Task be_healthy_when_oracle_is_available()
3434
[Fact]
3535
public async Task be_unhealthy_when_oracle_is_not_available()
3636
{
37-
var connectionString = "Data Source=255.255.255.255:1521/XEPDB1;User Id=system;Password=oracle";
37+
var connectionStringBuilder = oracleFixture.GetConnectionStringBuilder();
38+
39+
connectionStringBuilder.DataSource = "invalid";
3840

3941
var webHostBuilder = new WebHostBuilder()
4042
.ConfigureServices(services =>
4143
{
4244
services.AddHealthChecks()
43-
.AddOracle(connectionString, tags: ["oracle"]);
45+
.AddOracle(connectionStringBuilder.ConnectionString, tags: ["oracle"]);
4446
})
4547
.Configure(app =>
4648
{
@@ -58,7 +60,8 @@ public async Task be_unhealthy_when_oracle_is_not_available()
5860
[Fact]
5961
public async Task be_unhealthy_when_sql_query_is_not_valid()
6062
{
61-
var connectionString = "Data Source=localhost:1521/XEPDB1;User Id=system;Password=oracle";
63+
string connectionString = oracleFixture.GetConnectionString();
64+
6265
var webHostBuilder = new WebHostBuilder()
6366
.ConfigureServices(services =>
6467
{
@@ -82,7 +85,7 @@ public async Task be_unhealthy_when_sql_query_is_not_valid()
8285
public async Task be_healthy_with_connection_string_factory_when_oracle_is_available()
8386
{
8487
bool factoryCalled = false;
85-
string connectionString = "Data Source=localhost:1521/XEPDB1;User Id=system;Password=oracle";
88+
string connectionString = oracleFixture.GetConnectionString();
8689

8790
var webHostBuilder = new WebHostBuilder()
8891
.ConfigureServices(services =>
@@ -114,8 +117,8 @@ public async Task be_healthy_with_connection_string_factory_when_oracle_is_avail
114117
public async Task be_healthy_with_connection_string_and_credential_when_oracle_is_available()
115118
{
116119
bool factoryCalled = false;
117-
string connectionString = "Data Source=localhost:1521/XEPDB1";
118-
var password = new NetworkCredential("system", "oracle").SecurePassword;
120+
var connectionStringBuilder = oracleFixture.GetConnectionStringBuilder();
121+
var password = new NetworkCredential(connectionStringBuilder.UserID, connectionStringBuilder.Password).SecurePassword;
119122
password.MakeReadOnly();
120123
var credential = new OracleCredential("system", password);
121124

@@ -124,7 +127,7 @@ public async Task be_healthy_with_connection_string_and_credential_when_oracle_i
124127
{
125128
services
126129
.AddHealthChecks()
127-
.AddOracle(connectionString, tags: ["oracle"],
130+
.AddOracle($"DATA SOURCE={connectionStringBuilder.DataSource}", tags: ["oracle"],
128131
configure: options =>
129132
{
130133
factoryCalled = true;

test/HealthChecks.Oracle.Tests/HealthChecks.Oracle.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.Oracle\HealthChecks.Oracle.csproj" />
55
</ItemGroup>
66

7+
<ItemGroup>
8+
<PackageReference Include="Testcontainers.Oracle" />
9+
</ItemGroup>
10+
711
</Project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Oracle.ManagedDataAccess.Client;
2+
using Testcontainers.Oracle;
3+
4+
namespace HealthChecks.Oracle.Tests;
5+
6+
public class OracleContainerFixture : IAsyncLifetime
7+
{
8+
private const string Registry = "docker.io";
9+
10+
private const string Image = "gvenzl/oracle-xe";
11+
12+
private const string Tag = "21.3.0-slim-faststart";
13+
14+
public OracleContainer? Container { get; private set; }
15+
16+
public string GetConnectionString()
17+
{
18+
if (Container is null)
19+
{
20+
throw new InvalidOperationException("The test container was not initialized.");
21+
}
22+
23+
return Container.GetConnectionString();
24+
}
25+
26+
public OracleConnectionStringBuilder GetConnectionStringBuilder() => new(GetConnectionString());
27+
28+
public async Task InitializeAsync() => Container = await CreateContainerAsync();
29+
30+
public Task DisposeAsync() => Container?.DisposeAsync().AsTask() ?? Task.CompletedTask;
31+
32+
private async Task<OracleContainer> CreateContainerAsync()
33+
{
34+
var container = new OracleBuilder()
35+
.WithImage($"{Registry}/{Image}:{Tag}")
36+
.Build();
37+
38+
await container.StartAsync();
39+
40+
return container;
41+
}
42+
}

0 commit comments

Comments
 (0)