Skip to content

Commit

Permalink
added test containers and simplified pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
juuwel committed Apr 25, 2024
1 parent 24a9ac7 commit e470010
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 56 deletions.
47 changes: 1 addition & 46 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,62 +12,17 @@ on:
jobs:
tests:
runs-on: ubuntu-latest

# Service containers to run with `container-job`
services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres
# Provide the password for postgres
env:
POSTGRES_PASSWORD: postgres
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: Start PostgreSQL on Ubuntu
run: |
sudo systemctl start postgresql.service
pg_isready
- name: Create additional user
run: |
sudo -u postgres psql --command="CREATE USER testuser PASSWORD 'password'" --command="\du"
- name: Create additional database
run: |
sudo -u postgres createdb TestDatabase --owner=testuser
PGPASSWORD=password
- name: Enable uuid-ossp extension
run: |
echo "CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";" | sudo -u postgres psql TestDatabase
- name: Checkout
uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x

- name: Create database
env:
REMOTE_TESTING: "true"
BotaniqueDb: Host=localhost;Database=TestDatabase;Username=testuser;Password=password
run: cd api &&
dotnet run &
(sleep 90 && kill -9 $(lsof -t -i:5141))

- name: Run tests
env:
BotaniqueDb: Host=localhost;Database=TestDatabase;Username=testuser;Password=password
run: cd Tests && dotnet test --no-build
run: cd Tests && dotnet test



Expand Down
2 changes: 1 addition & 1 deletion Tests/GlobalTestSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class GlobalTestSetup
[OneTimeSetUp]
public async Task StartApi()
{
await Startup.StartApi(["ENVIRONMENT=Development"]);
await Startup.StartApi(["Testing=true", "--db-init"]);
}
}
40 changes: 31 additions & 9 deletions api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Shared.Dtos.FromClient;
using Shared.Models;
using Shared.Models.Exceptions;
using Testcontainers.PostgreSql;

namespace api;

Expand Down Expand Up @@ -41,21 +42,43 @@ public static async Task<WebApplication> StartApi(string[] args)

var builder = WebApplication.CreateBuilder(args);

var connectionString = builder.Configuration.GetConnectionString("BotaniqueDb");
builder.Services.AddDbContextFactory<ApplicationDbContext>(options =>
if (args.Contains("Testing=true"))
{
connectionString ??= Environment.GetEnvironmentVariable("BotaniqueDb");
options.UseNpgsql(connectionString ?? throw new Exception("Connection string cannot be null"));
});
var dbContainer =
new PostgreSqlBuilder()
.WithDatabase("botanique")
.WithUsername("root")
.WithPassword("password")
.Build();

await dbContainer.StartAsync();

var connectionString = dbContainer.GetConnectionString();
builder.Services.AddDbContextFactory<ApplicationDbContext>(options =>
{
options.UseNpgsql(connectionString ?? throw new Exception("Connection string cannot be null"));
});
}

else
{
var connectionString = builder.Configuration.GetConnectionString("BotaniqueDb");
builder.Services.AddDbContextFactory<ApplicationDbContext>(options =>
{
connectionString ??= Environment.GetEnvironmentVariable("BotaniqueDb");
options.UseNpgsql(connectionString ?? throw new Exception("Connection string cannot be null"));
});
}

builder.Services.Configure<JwtOptions>(builder.Configuration.GetSection("JWT"));
builder.Services.Configure<MqttOptions>(builder.Configuration.GetSection("MQTT"));
builder.Services.AddServicesAndRepositories();

var services = builder.FindAndInjectClientEventHandlers(Assembly.GetExecutingAssembly());

var app = builder.Build();

if (args.Contains("--db-init") || Environment.GetEnvironmentVariable("REMOTE_TESTING") is not null)
if (args.Contains("--db-init"))
{
var scope = app.Services.CreateScope();
var db = app.Services.GetRequiredService<IDbContextFactory<ApplicationDbContext>>().CreateDbContext();
Expand All @@ -72,10 +95,9 @@ await userRepository.CreateUser(new RegisterUserDto
});
}

// builder.WebHost.UseUrls("http://*:9999");

var port = Environment.GetEnvironmentVariable("PORT") ?? "8181";
var wsServer = new WebSocketServer($"ws://0.0.0.0:{port}");
// builder.WebHost.UseUrls("http://*:9999");

wsServer.Start(socket =>
{
Expand Down
1 change: 1 addition & 0 deletions api/api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageReference Include="Serilog" Version="3.1.1"/>
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
<PackageReference Include="Testcontainers.PostgreSql" Version="3.8.0" />
<PackageReference Include="uldahlalex.websocket.boilerplate" Version="1.6.0"/>
</ItemGroup>

Expand Down

0 comments on commit e470010

Please sign in to comment.