Skip to content

Commit

Permalink
Docker container tests with RabbitMQ
Browse files Browse the repository at this point in the history
  • Loading branch information
duracellko committed Aug 18, 2024
1 parent ac5b697 commit f8b38a6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 60 deletions.
45 changes: 6 additions & 39 deletions docker/test/RunTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ Param (

$projectPath = $PSScriptRoot
$pesterVersion = '5.6.1'
$redisVersion = '7.4'
$rabbitmqVersion = '3.13'

$imageTag = 'local-test'
if (![string]::IsNullOrEmpty($PlanningPokerImageTag)) {
$imageTag = $PlanningPokerImageTag
}

$composeProjectName = 'planningpoker'
$redisAppPassword = (New-Guid).ToString()
$redisAdminPassword = (New-Guid).ToString()
$rabbitmqPassword = (New-Guid).ToString()
$applicationPorts = @(5001, 5002, 5003)

function RandomizeApplicationPorts() {
Expand All @@ -31,45 +30,15 @@ function SetupEnvironmentVariables() {
[Parameter(Mandatory = $true)]
[int[]] $ApplicationPorts,
[Parameter(Mandatory = $true)]
[string] $AppPassword
[string] $RabbitMQPassword
)

$env:PLANNINGPOKER_IMAGENAME = 'duracellko/planningpoker:' + $AppImageTag
$env:PLANNINGPOKER_APP1_PORT = $ApplicationPorts[0]
$env:PLANNINGPOKER_APP2_PORT = $ApplicationPorts[1]
$env:PLANNINGPOKER_APP3_PORT = $ApplicationPorts[2]
$env:PLANNINGPOKER_APP_REDIS_PASSWORD = $AppPassword
$env:PLANNINGPOKER_REDIS_VERSION = $redisVersion
}

function PrepareRedisConfiguration() {
param (
[Parameter(Mandatory = $true)]
[string] $AppPassword,
[Parameter(Mandatory = $true)]
[string] $AdminPassword
)

$configurationPath = Join-Path -Path $projectPath -ChildPath 'redis.conf'
$configuration = Get-Content -Path $configurationPath -Raw
$configuration = $configuration.Replace('${PLANNINGPOKER_ADMIN_PASSWORD}', $AdminPassword)
$configuration = $configuration.Replace('${PLANNINGPOKER_APP_PASSWORD}', $AppPassword)
Set-Content -Path $configurationPath -Value $configuration -NoNewline
}

function RevertRedisConfiguration() {
param (
[Parameter(Mandatory = $true)]
[string] $AppPassword,
[Parameter(Mandatory = $true)]
[string] $AdminPassword
)

$configurationPath = Join-Path -Path $projectPath -ChildPath 'redis.conf'
$configuration = Get-Content -Path $configurationPath -Raw
$configuration = $configuration.Replace($AdminPassword, '${PLANNINGPOKER_ADMIN_PASSWORD}')
$configuration = $configuration.Replace($AppPassword, '${PLANNINGPOKER_APP_PASSWORD}')
Set-Content -Path $configurationPath -Value $configuration -NoNewline
$env:PLANNINGPOKER_APP_RABBITMQ_PASSWORD = $RabbitMQPassword
$env:PLANNINGPOKER_RABBITMQ_VERSION = $rabbitmqVersion
}

function ComposeDockerUp() {
Expand Down Expand Up @@ -118,8 +87,7 @@ $composeFilePath = Join-Path -Path $projectPath -ChildPath 'compose.yml'

try {
RandomizeApplicationPorts
SetupEnvironmentVariables -AppImageTag $imageTag -ApplicationPorts $applicationPorts -AppPassword $redisAppPassword
PrepareRedisConfiguration -AppPassword $redisAppPassword -AdminPassword $redisAdminPassword
SetupEnvironmentVariables -AppImageTag $imageTag -ApplicationPorts $applicationPorts -RabbitMQPassword $rabbitmqPassword

ComposeDockerUp -ComposePath $composeFilePath -ProjectName $composeProjectName

Expand Down Expand Up @@ -157,6 +125,5 @@ try {
}
}
finally {
RevertRedisConfiguration -AppPassword $redisAppPassword -AdminPassword $redisAdminPassword
ComposeDockerDown -ComposePath $composeFilePath -ProjectName $composeProjectName
}
35 changes: 21 additions & 14 deletions docker/test/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ services:
planningpoker-r1:
image: ${PLANNINGPOKER_IMAGENAME:-duracellko/planningpoker:local-test}
depends_on:
- redis
rabbitmq:
condition: service_healthy
environment:
PlanningPoker__ServiceBusConnectionString: "REDIS:redis,user=planningpoker,password=${PLANNINGPOKER_APP_REDIS_PASSWORD}"
PlanningPoker__ServiceBusConnectionString: "RABBITMQ:amqp://planningpoker:${PLANNINGPOKER_APP_RABBITMQ_PASSWORD}@rabbitmq/"
PlanningPoker__InitializationMessageTimeout: 3
PlanningPokerClient__UseHttpClient: true
ports:
Expand All @@ -13,9 +14,10 @@ services:
planningpoker-r2:
image: ${PLANNINGPOKER_IMAGENAME:-duracellko/planningpoker:local-test}
depends_on:
- redis
rabbitmq:
condition: service_healthy
environment:
PlanningPoker__ServiceBusConnectionString: "REDIS:redis,user=planningpoker,password=${PLANNINGPOKER_APP_REDIS_PASSWORD}"
PlanningPoker__ServiceBusConnectionString: "RABBITMQ:amqp://planningpoker:${PLANNINGPOKER_APP_RABBITMQ_PASSWORD}@rabbitmq/"
PlanningPoker__InitializationMessageTimeout: 3
PlanningPokerClient__UseHttpClient: true
ports:
Expand All @@ -24,19 +26,24 @@ services:
planningpoker-r3:
image: ${PLANNINGPOKER_IMAGENAME:-duracellko/planningpoker:local-test}
depends_on:
- redis
rabbitmq:
condition: service_healthy
environment:
PlanningPoker__ServiceBusConnectionString: "REDIS:redis,user=planningpoker,password=${PLANNINGPOKER_APP_REDIS_PASSWORD}"
PlanningPoker__ServiceBusConnectionString: "RABBITMQ:amqp://planningpoker:${PLANNINGPOKER_APP_RABBITMQ_PASSWORD}@rabbitmq/"
PlanningPoker__InitializationMessageTimeout: 3
PlanningPokerClient__UseHttpClient: true
ports:
- "${PLANNINGPOKER_APP3_PORT:-5003}:8080"

redis:
image: redis:${PLANNINGPOKER_REDIS_VERSION:-latest}
volumes:
- type: bind
source: ./redis.conf
target: /usr/local/etc/redis/redis.conf
read_only: true
command: [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
rabbitmq:
image: rabbitmq:${PLANNINGPOKER_RABBITMQ_VERSION:-latest}
environment:
RABBITMQ_DEFAULT_USER: planningpoker
RABBITMQ_DEFAULT_PASS: ${PLANNINGPOKER_APP_RABBITMQ_PASSWORD}
healthcheck:
test: "rabbitmq-diagnostics -q check_running && rabbitmq-diagnostics -q check_local_alarms && rabbitmq-diagnostics -q check_port_listener 5672"
interval: 30s
timeout: 10s
retries: 3
start_period: 20s
start_interval: 2s
3 changes: 0 additions & 3 deletions docker/test/redis.conf

This file was deleted.

24 changes: 20 additions & 4 deletions src/Duracellko.PlanningPoker.Web/AzurePlanningPokerNodeService.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
using Duracellko.PlanningPoker.Azure;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Duracellko.PlanningPoker.Web;

public sealed class AzurePlanningPokerNodeService : IHostedService, IDisposable
{
private static readonly Action<ILogger, Exception?> _logErrorStart = LoggerMessage.Define(
LogLevel.Error,
new EventId(0, "ErrorStartAzurePlanningPokerNode"),
"Starting Azure PlanningPoker Node failed.");

private readonly PlanningPokerAzureNode _node;
private readonly ILogger _logger;

public AzurePlanningPokerNodeService(PlanningPokerAzureNode node)
public AzurePlanningPokerNodeService(PlanningPokerAzureNode node, ILogger<AzurePlanningPokerNodeService> logger)
{
_node = node ?? throw new ArgumentNullException(nameof(node));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

public Task StartAsync(CancellationToken cancellationToken)
[SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Log error and continue without Azure node service.")]
public async Task StartAsync(CancellationToken cancellationToken)
{
_node.Start();
return Task.CompletedTask;
try
{
await _node.Start();
}
catch (Exception ex)
{
_logErrorStart(_logger, ex);
}
}

public Task StopAsync(CancellationToken cancellationToken)
Expand Down

0 comments on commit f8b38a6

Please sign in to comment.