Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Integration tests] Add docker step for test containers #278

Merged
merged 1 commit into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 33 additions & 16 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ jobs:
params="/d:sonar.pullrequest.key=${pr_number}"
fi
../.sonar/scanner/dotnet-sonarscanner begin /k:"zarusz_SlimMessageBus" /o:"zarusz" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.opencover.reportsPaths="**/coverage.opencover.xml" /d:sonar.exclusions="Samples/**/*,Tests/**/*" $params
#run: ../.sonar/scanner/dotnet-sonarscanner begin /k:"zarusz_SlimMessageBus" /o:"zarusz" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.opencover.reportsPaths="**/coverage.opencover.xml" /d:sonar.exclusions="Samples/**/*,Tests/**/*" /d:sonar.pullrequest.key="${{ github.event.pull_request.number }}" /d:sonar.pullrequest.branch="new-branch"
working-directory: ./src

- name: Build
Expand All @@ -98,11 +97,16 @@ jobs:
run: dotnet test $SOLUTION_NAME --configuration $SOLUTION_CONFIGURATION --no-build --verbosity normal --logger html --results-directory TestResults --collect:"XPlat Code Coverage;Format=opencover" --filter Category!=Integration
working-directory: ./src

- name: Integrations Tests
- name: Integration Tests - Infrastructure
run: docker compose -f src/Infrastructure/docker-compose.yml up --detach --force-recreate -V

- name: Integration Tests
# Run integration tests against the test infrastructure if secrets are provided
run: dotnet test $SOLUTION_NAME --configuration $SOLUTION_CONFIGURATION --no-build --verbosity normal --logger html --results-directory TestResults --collect:"XPlat Code Coverage;Format=opencover" --filter Category=Integration
#run: dotnet test $SOLUTION_NAME --configuration $SOLUTION_CONFIGURATION --no-build --verbosity normal --logger html --results-directory TestResults --collect:"XPlat Code Coverage;Format=opencover" --filter "Category=Integration&Transport=Outbox"
working-directory: ./src
env:
# Connects to the Azure cloud
azure_servicebus_connectionstring: ${{ secrets.azure_servicebus_connectionstring }}

azure_eventhub_connectionstring: ${{ secrets.azure_eventhub_connectionstring }}
Expand All @@ -113,24 +117,37 @@ jobs:
kafka_password: ${{ secrets.kafka_password }}
kafka_secure: ${{ secrets.kafka_secure }}

mqtt_server: ${{ secrets.mqtt_server }}
mqtt_port: ${{ secrets.mqtt_port }}
mqtt_username: ${{ secrets.mqtt_username }}
mqtt_password: ${{ secrets.mqtt_password }}
mqtt_secure: ${{ secrets.mqtt_secure }}
_mqtt_server: ${{ secrets.mqtt_server }}
_mqtt_port: ${{ secrets.mqtt_port }}
_mqtt_username: ${{ secrets.mqtt_username }}
_mqtt_password: ${{ secrets.mqtt_password }}
_mqtt_secure: ${{ secrets.mqtt_secure }}

_rabbitmq_connectionstring: ${{ secrets.rabbitmq_connectionstring }}

_redis_connectionstring: ${{ secrets.redis_connectionstring }}

_sqlserver_connectionstring: ${{ secrets.sqlserver_connectionstring }}

# Connects to the local Test Containers

# need to postpone until feature https://github.com/zarusz/SlimMessageBus/issues/110 is implemented
_kafka_brokers: localhost:9092
_kafka_username: user
_kafka_password: password
_kafka_secure: false

sqlserver_connectionstring: ${{ secrets.sqlserver_connectionstring }}
mqtt_server: localhost
mqtt_port: 1883
mqtt_username: "(empty)"
mqtt_password: "(empty)"
mqtt_secure: false

rabbitmq_connectionstring: ${{ secrets.rabbitmq_connectionstring }}
rabbitmq_connectionstring: amqp://localhost

redis_connectionstring: ${{ secrets.redis_connectionstring }}
redis_connectionstring: localhost:6379

azure_servicebus_key: ${{ secrets.azure_servicebus_key }}
azure_eventhub_key: ${{ secrets.azure_eventhub_key }}
azure_eventhub_storage_key: ${{ secrets.azure_eventhub_storage_key }}
azure_sql_password: ${{ secrets.azure_sql_password }}
redis_password: ${{ secrets.redis_password }}
rabbitmq_password: ${{ secrets.rabbitmq_password }}
sqlserver_connectionstring: "Server=localhost;Initial Catalog=SlimMessageBus_Outbox;User ID=sa;Password=SuperSecretP@55word;TrustServerCertificate=true;MultipleActiveResultSets=true;"

- name: SonarCloud - SonarScanner End
#if: github.event_name == 'pull_request_target'
Expand Down
14 changes: 12 additions & 2 deletions src/SlimMessageBus.sln
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlimMessageBus.Host.Serialization.Hybrid.Test", "Tests\SlimMessageBus.Host.Serialization.Hybrid.Test\SlimMessageBus.Host.Serialization.Hybrid.Test.csproj", "{DB624D5F-CB7C-4E16-B1E2-3B368FCB5A46}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlimMessageBus.Host.Outbox.Test", "Tests\SlimMessageBus.Host.Outbox.Test\SlimMessageBus.Host.Outbox.Test.csproj", "{AD05234E-A925-44C0-977E-FEAC2A75B98C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Infrastructure", "Infrastructure", "{137BFD32-CD0A-47CA-8884-209CD49DEE8C}"
ProjectSection(SolutionItems) = preProject
Infrastructure\docker-compose.yml = Infrastructure\docker-compose.yml
Expand All @@ -259,6 +260,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{1A71BB05
..\build\tasks.ps1 = ..\build\tasks.ps1
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SecretStore.Test", "Tests\SecretStore.Test\SecretStore.Test.csproj", "{969AAB37-AEFC-40F9-9F89-B4B5E45E13C9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -779,6 +782,14 @@ Global
{AD05234E-A925-44C0-977E-FEAC2A75B98C}.Release|Any CPU.Build.0 = Release|Any CPU
{AD05234E-A925-44C0-977E-FEAC2A75B98C}.Release|x86.ActiveCfg = Release|Any CPU
{AD05234E-A925-44C0-977E-FEAC2A75B98C}.Release|x86.Build.0 = Release|Any CPU
{969AAB37-AEFC-40F9-9F89-B4B5E45E13C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{969AAB37-AEFC-40F9-9F89-B4B5E45E13C9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{969AAB37-AEFC-40F9-9F89-B4B5E45E13C9}.Debug|x86.ActiveCfg = Debug|Any CPU
{969AAB37-AEFC-40F9-9F89-B4B5E45E13C9}.Debug|x86.Build.0 = Debug|Any CPU
{969AAB37-AEFC-40F9-9F89-B4B5E45E13C9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{969AAB37-AEFC-40F9-9F89-B4B5E45E13C9}.Release|Any CPU.Build.0 = Release|Any CPU
{969AAB37-AEFC-40F9-9F89-B4B5E45E13C9}.Release|x86.ActiveCfg = Release|Any CPU
{969AAB37-AEFC-40F9-9F89-B4B5E45E13C9}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -855,8 +866,7 @@ Global
{8507237C-68C3-46AD-B7DA-800791C6FDDB} = {9F005B5C-A856-4351-8C0C-47A8B785C637}
{DB624D5F-CB7C-4E16-B1E2-3B368FCB5A46} = {9F005B5C-A856-4351-8C0C-47A8B785C637}
{AD05234E-A925-44C0-977E-FEAC2A75B98C} = {9F005B5C-A856-4351-8C0C-47A8B785C637}
{8507237C-68C3-46AD-B7DA-800791C6FDDB} = {9F005B5C-A856-4351-8C0C-47A8B785C637}
{DB624D5F-CB7C-4E16-B1E2-3B368FCB5A46} = {9F005B5C-A856-4351-8C0C-47A8B785C637}
{969AAB37-AEFC-40F9-9F89-B4B5E45E13C9} = {D3D6FD9A-968A-45BB-86C7-4527C72A057E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {435A0D65-610C-4B84-B1AA-2C7FBE72DB80}
Expand Down
22 changes: 22 additions & 0 deletions src/Tests/SecretStore.Test/EnvironmentVariableSecretStoreTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace SecretStore.Test;

public class EnvironmentVariableSecretStoreTests
{
[Fact]
public void Given_EnvWithEmptyToken_When_GetSecret_Then_ReturnsEmptyString()
{
// Arrange
Environment.SetEnvironmentVariable("kafka_username", "(empty)");
Environment.SetEnvironmentVariable("mqtt_username", "R2D2");

var secretStore = new EnvironmentVariableSecretStore();

// Actr
var kafkaUsername = secretStore.GetSecret("kafka_username");
var mqttUsername = secretStore.GetSecret("mqtt_username");

// Assert
kafkaUsername.Should().Be(string.Empty);
mqttUsername.Should().Be("R2D2");
}
}
31 changes: 31 additions & 0 deletions src/Tests/SecretStore.Test/FileSecretStoreTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace SecretStore.Test;

public class FileSecretStoreTests
{
[Fact]
public void Given_TextFileWithKeyValuePairsSeparatedByEqualsSign_When_Constructor_Then_GetSecretMethodReturnsProperValue()
{
// Arrange
var fileContents = "kafka_username=JohnDoe\nkafka_username=JohnWick\n#commented mqtt_username=123\n mqtt_username=";
var tempFilePath = Path.GetTempFileName();
File.WriteAllText(tempFilePath, fileContents);
try
{
var secretStore = new FileSecretStore(tempFilePath);

// Act
var kafkaUsername = secretStore.GetSecret("kafka_username");
var mqttUsername = secretStore.GetSecret("mqtt_username");

// Assert
kafkaUsername.Should().Be("JohnWick");
mqttUsername.Should().Be(string.Empty);

}
finally
{
// Clean up
File.Delete(tempFilePath);
}
}
}
9 changes: 9 additions & 0 deletions src/Tests/SecretStore.Test/SecretStore.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="../Host.Test.Properties.xml" />

<ItemGroup>
<ProjectReference Include="..\..\Tools\SecretStore\SecretStore.csproj" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions src/Tests/SecretStore.Test/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global using FluentAssertions;

global using Xunit;
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ namespace SlimMessageBus.Host.Kafka.Test;
/// <code>bin\windows\kafka-topics.bat --create --zookeeper localhost:2181 --partitions 2 --replication-factor 1 --topic test-echo</code>
/// </remarks>
/// </summary>
[Trait("Category", "Integration")]
[Trait("Category", "Integration")]
[Trait("Transport", "Kafka")]
public class KafkaMessageBusIt : BaseIntegrationTest<KafkaMessageBusIt>
{
private const int NumberOfMessages = 77;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace SlimMessageBus.Host.Mqtt.Test;
using System.Diagnostics;

[Trait("Category", "Integration")]
[Trait("Transport", "Mqtt")]
public class MqttMessageBusIt : BaseIntegrationTest<MqttMessageBusIt>
{
private const int NumberOfMessages = 77;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
/// </summary>
/// <param name="testOutputHelper"></param>
[Trait("Category", "Integration")] // for benchmarks
[Trait("Transport", "Outbox")]
[Collection(CustomerContext.Schema)]
public class OutboxBenchmarkTests(ITestOutputHelper testOutputHelper) : BaseIntegrationTest<OutboxBenchmarkTests>(testOutputHelper)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

[Trait("Category", "Integration")]
[Collection(CustomerContext.Schema)]
[Trait("Transport", "Outbox")]
public class OutboxTests(ITestOutputHelper testOutputHelper) : BaseIntegrationTest<OutboxTests>(testOutputHelper)
{
private TransactionType _testParamTransactionType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ namespace SlimMessageBus.Host.RabbitMQ.Test.IntegrationTests;
using SlimMessageBus.Host.Test.Common.IntegrationTest;

[Trait("Category", "Integration")]
public class RabbitMqMessageBusIt(ITestOutputHelper testOutputHelper) : BaseIntegrationTest<RabbitMqMessageBusIt>(testOutputHelper)
[Trait("Transport", "RabbitMQ")]
public class RabbitMqMessageBusIt(ITestOutputHelper testOutputHelper)
: BaseIntegrationTest<RabbitMqMessageBusIt>(testOutputHelper)
{
private const int NumberOfMessages = 144;

Expand Down
8 changes: 3 additions & 5 deletions src/Tests/SlimMessageBus.Host.Redis.Test/RedisMessageBusIt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ namespace SlimMessageBus.Host.Redis.Test;
using SlimMessageBus.Host.Test.Common.IntegrationTest;

[Trait("Category", "Integration")]
public class RedisMessageBusIt : BaseIntegrationTest<RedisMessageBusIt>
[Trait("Transport", "Redis")]
public class RedisMessageBusIt(ITestOutputHelper testOutputHelper)
: BaseIntegrationTest<RedisMessageBusIt>(testOutputHelper)
{
private const int NumberOfMessages = 77;

public RedisMessageBusIt(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
{
}

protected override void SetupServices(ServiceCollection services, IConfigurationRoot configuration)
{
services.AddSlimMessageBus(mbb =>
Expand Down
11 changes: 6 additions & 5 deletions src/Tools/SecretStore/EnvironmentVariableSecretStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

public class EnvironmentVariableSecretStore : ISecretStore
{
#region Implementation of ISecretStore

public string GetSecret(string name)
{
return Environment.GetEnvironmentVariable(name);
var value = Environment.GetEnvironmentVariable(name);
if (value == "(empty)")
{
return string.Empty;
}
return value;
}

#endregion
}
7 changes: 4 additions & 3 deletions src/Tools/SecretStore/FileSecretStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ public FileSecretStore(string path)

var lines = File.ReadAllLines(path);
_secrets = lines
.Where(x => !string.IsNullOrWhiteSpace(x) || x!.TrimStart().StartsWith('#'))
.Select(x => x.Split('=', 2, StringSplitOptions.RemoveEmptyEntries))
.ToDictionary(x => x[0], x => x.Length == 2 ? x[1] : string.Empty);
.Where(x => !string.IsNullOrWhiteSpace(x) && !x!.TrimStart().StartsWith('#')) // skip empty lines or starting with a comment #
.Select(x => x.Split('=', 2).Select(i => i.Trim()).ToArray())
.GroupBy(x => x[0], x => x.Length == 2 ? x[1] : string.Empty)
.ToDictionary(x => x.Key, x => x.LastOrDefault()); // take the last value for the key
}

public string GetSecret(string name)
Expand Down
Loading