Skip to content

Commit

Permalink
Add simple E2E test
Browse files Browse the repository at this point in the history
  • Loading branch information
Syriiin committed Nov 21, 2023
1 parent 6a8b28c commit e2a453a
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/test-on-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- run: make test

e2e-test:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- run: make test-e2e
2 changes: 2 additions & 0 deletions Difficalcy.Catch/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public Startup(IConfiguration configuration) : base(configuration) { }

public override string OpenApiVersion => "v1";

protected override string TestBeatmapAssembly => "osu.Game.Rulesets.Catch";

public override void ConfigureCalculatorServices(IServiceCollection services)
{
services.AddSingleton(typeof(CatchCalculatorService));
Expand Down
2 changes: 2 additions & 0 deletions Difficalcy.Mania/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public Startup(IConfiguration configuration) : base(configuration) { }

public override string OpenApiVersion => "v1";

protected override string TestBeatmapAssembly => "osu.Game.Rulesets.Mania";

public override void ConfigureCalculatorServices(IServiceCollection services)
{
services.AddSingleton(typeof(ManiaCalculatorService));
Expand Down
2 changes: 2 additions & 0 deletions Difficalcy.Osu/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public Startup(IConfiguration configuration) : base(configuration) { }

public override string OpenApiVersion => "v1";

protected override string TestBeatmapAssembly => "osu.Game.Rulesets.Osu";

public override void ConfigureCalculatorServices(IServiceCollection services)
{
services.AddSingleton(typeof(OsuCalculatorService));
Expand Down
2 changes: 2 additions & 0 deletions Difficalcy.Taiko/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public Startup(IConfiguration configuration) : base(configuration) { }

public override string OpenApiVersion => "v1";

protected override string TestBeatmapAssembly => "osu.Game.Rulesets.Taiko";

public override void ConfigureCalculatorServices(IServiceCollection services)
{
services.AddSingleton(typeof(TaikoCalculatorService));
Expand Down
13 changes: 11 additions & 2 deletions Difficalcy/DifficalcyStartup.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Difficalcy.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand All @@ -15,6 +16,11 @@ abstract public class DifficalcyStartup

public abstract string OpenApiVersion { get; }

/// <summary>
/// The Assembly to source test beatmap resources from (eg. osu.Game.Rulesets.Osu)
/// </summary>
protected abstract string TestBeatmapAssembly { get; }

public DifficalcyStartup(IConfiguration configuration)
{
Configuration = configuration;
Expand All @@ -32,15 +38,18 @@ public void ConfigureServices(IServiceCollection services)
});

var redisConfig = Configuration["REDIS_CONFIGURATION"];

ICache cache;
if (redisConfig == null)
cache = new DummyCache();
else
cache = new RedisCache(ConnectionMultiplexer.Connect(redisConfig));
services.AddSingleton<ICache>(cache);

services.AddSingleton(typeof(IBeatmapProvider), typeof(WebBeatmapProvider));
var useTestBeatmapProvider = Configuration["USE_TEST_BEATMAP_PROVIDER"];
if (useTestBeatmapProvider == "true")
services.AddSingleton<IBeatmapProvider>(new TestBeatmapProvider(TestBeatmapAssembly));
else
services.AddSingleton(typeof(IBeatmapProvider), typeof(WebBeatmapProvider));

ConfigureCalculatorServices(services);
}
Expand Down
44 changes: 44 additions & 0 deletions Difficalcy/Services/TestBeatmapProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;

namespace Difficalcy.Services
{
public class TestBeatmapProvider : IBeatmapProvider
{
private string _resourceAssemblyName;

public TestBeatmapProvider(string resourceAssemblyName)
{
_resourceAssemblyName = resourceAssemblyName;
}

public Task<bool> EnsureBeatmap(string beatmapId)
{
var resourceName = $"{_resourceAssemblyName}.Resources.{beatmapId}";
var info = ResourceAssembly.GetManifestResourceInfo(resourceName);
return Task.FromResult(info != null);
}

public Stream GetBeatmapStream(string beatmapId)
{
var resourceNamespace = "Testing.Beatmaps";
var resourceName = $"{resourceNamespace}.{beatmapId}.osu";
var fullResourceName = $"{_resourceAssemblyName}.Resources.{resourceName}";
var stream = ResourceAssembly.GetManifestResourceStream(fullResourceName);
if (stream == null)
throw new Exception($@"Unable to find resource ""{fullResourceName}"" in assembly ""{_resourceAssemblyName}""");
return stream;
}

private Assembly ResourceAssembly
{
get
{
string localPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!;
return Assembly.LoadFrom(Path.Combine(localPath, $"{_resourceAssemblyName}.dll"));
}
}
}
}
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
COMPOSE_RUN_TOOLING = docker compose -f docker-compose.tooling.yml run --rm --build tooling
COMPOSE_TOOLING_RUN = docker compose -f docker-compose.tooling.yml run --rm --build tooling
COMPOSE_E2E = docker compose -f docker-compose.e2e.yml
COMPOSE_E2E_RUN = $(COMPOSE_E2E) run --rm --build e2e-test-runner
COMPOSE_APP_DEV = docker compose -f docker-compose.yml

help: ## Show this help
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'

bash: ## Opens bash shell in tooling container
$(COMPOSE_RUN_TOOLING) bash
$(COMPOSE_TOOLING_RUN) bash

test: ## Runs test suite
$(COMPOSE_RUN_TOOLING) dotnet test
$(COMPOSE_TOOLING_RUN) dotnet test

test-e2e: ## Runs E2E test suite
$(COMPOSE_E2E_RUN)
$(COMPOSE_E2E) down --remove-orphans

build-dev: ## Builds development docker images
$(COMPOSE_APP_DEV) build
Expand Down
61 changes: 61 additions & 0 deletions docker-compose.e2e.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
services:
e2e-test-runner:
image: alpine
build: ./e2e
environment:
- DIFFICALCY_OSU_HOST=e2e-difficalcy-osu:80
- DIFFICALCY_TAIKO_HOST=e2e-difficalcy-taiko:80
- DIFFICALCY_CATCH_HOST=e2e-difficalcy-catch:80
- DIFFICALCY_MANIA_HOST=e2e-difficalcy-mania:80
depends_on:
- e2e-difficalcy-osu
- e2e-difficalcy-taiko
- e2e-difficalcy-catch
- e2e-difficalcy-mania

e2e-difficalcy-osu:
build:
context: .
dockerfile: ./Difficalcy.Osu/Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT=Production
- USE_TEST_BEATMAP_PROVIDER=true
- REDIS_CONFIGURATION=cache:6379
depends_on:
- cache

e2e-difficalcy-taiko:
build:
context: .
dockerfile: ./Difficalcy.Taiko/Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT=Production
- USE_TEST_BEATMAP_PROVIDER=true
- REDIS_CONFIGURATION=cache:6379
depends_on:
- cache

e2e-difficalcy-catch:
build:
context: .
dockerfile: ./Difficalcy.Catch/Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT=Production
- USE_TEST_BEATMAP_PROVIDER=true
- REDIS_CONFIGURATION=cache:6379
depends_on:
- cache

e2e-difficalcy-mania:
build:
context: .
dockerfile: ./Difficalcy.Mania/Dockerfile
environment:
- ASPNETCORE_ENVIRONMENT=Production
- USE_TEST_BEATMAP_PROVIDER=true
- REDIS_CONFIGURATION=cache:6379
depends_on:
- cache

cache:
image: redis:6
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ services:
- REDIS_CONFIGURATION=cache:6379
volumes:
- ${BEATMAP_DIRECTORY}:/app/beatmaps/external
depends_on:
- cache

difficalcy-taiko:
image: difficalcy-taiko
Expand All @@ -26,6 +28,8 @@ services:
- REDIS_CONFIGURATION=cache:6379
volumes:
- ${BEATMAP_DIRECTORY}:/app/beatmaps/external
depends_on:
- cache

difficalcy-catch:
image: difficalcy-catch
Expand All @@ -40,6 +44,8 @@ services:
- REDIS_CONFIGURATION=cache:6379
volumes:
- ${BEATMAP_DIRECTORY}:/app/beatmaps/external
depends_on:
- cache

difficalcy-mania:
image: difficalcy-mania
Expand All @@ -54,6 +60,8 @@ services:
- REDIS_CONFIGURATION=cache:6379
volumes:
- ${BEATMAP_DIRECTORY}:/app/beatmaps/external
depends_on:
- cache

cache:
image: redis:6
9 changes: 9 additions & 0 deletions e2e/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM alpine:3

RUN apk add --no-cache bash curl jq

WORKDIR /e2e

COPY test .

CMD [ "./test" ]
14 changes: 14 additions & 0 deletions e2e/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
set -euo pipefail

curl --fail http://$DIFFICALCY_OSU_HOST/api/calculator/info | jq
curl --fail http://$DIFFICALCY_OSU_HOST/api/calculator/calculation?BeatmapId=diffcalc-test | jq

curl --fail http://$DIFFICALCY_CATCH_HOST/api/calculator/info | jq
curl --fail http://$DIFFICALCY_CATCH_HOST/api/calculator/calculation?BeatmapId=diffcalc-test | jq

curl --fail http://$DIFFICALCY_TAIKO_HOST/api/calculator/info | jq
curl --fail http://$DIFFICALCY_TAIKO_HOST/api/calculator/calculation?BeatmapId=diffcalc-test | jq

curl --fail http://$DIFFICALCY_MANIA_HOST/api/calculator/info | jq
curl --fail http://$DIFFICALCY_MANIA_HOST/api/calculator/calculation?BeatmapId=diffcalc-test | jq

0 comments on commit e2a453a

Please sign in to comment.