-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegrationTestsFactory.cs
83 lines (68 loc) · 3.29 KB
/
IntegrationTestsFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.IO;
using System.Threading.Tasks;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Containers;
using DotNet.Testcontainers.Networks;
namespace Demo.Tests.Config;
public class IntegrationTestsFactory : IAsyncLifetime
{
private const int SERVICE_BUS_PORT = 5672;
private readonly INetwork _containersNetwork;
private readonly IContainer _azureSqlEdgeContainer;
private readonly IContainer _serviceBusContainer;
public IntegrationTestsFactory()
{
_containersNetwork = new NetworkBuilder()
.WithName($"integration-tests-network-{Guid.NewGuid()}")
.Build();
const string SQL_PASSWORD = "Ab123456789!";
var sqlContainerName = $"sqledge-integration-tests-{Guid.NewGuid()}";
_azureSqlEdgeContainer = new ContainerBuilder()
.WithImage("mcr.microsoft.com/azure-sql-edge:2.0.0")
.WithEnvironment("ACCEPT_EULA", "Y")
.WithEnvironment("MSSQL_SA_PASSWORD", SQL_PASSWORD)
.WithNetwork(_containersNetwork)
.WithNetworkAliases(sqlContainerName)
.WithWaitStrategy(Wait.ForUnixContainer()
.UntilMessageIsLogged("SQL Server is now ready for client connections.", w => w.WithTimeout(TimeSpan.FromSeconds(10))))
.WithOutputConsumer(
Consume.RedirectStdoutAndStderrToStream(
Console.OpenStandardOutput(),
Console.OpenStandardError()))
.Build();
_serviceBusContainer = new ContainerBuilder()
.WithImage("mcr.microsoft.com/azure-messaging/servicebus-emulator:1.0.1")
.WithEnvironment("ACCEPT_EULA", "Y")
.WithEnvironment("MSSQL_SA_PASSWORD", SQL_PASSWORD)
.WithEnvironment("SQL_SERVER", sqlContainerName)
.WithPortBinding(SERVICE_BUS_PORT, true)
.WithBindMount(
Path.GetFullPath("./Config/ServiceBusEmulator.Config.json"),
"/ServiceBus_Emulator/ConfigFiles/Config.json")
.WithNetwork(_containersNetwork)
.WithWaitStrategy(Wait.ForUnixContainer()
.UntilMessageIsLogged("Emulator Service is Successfully Up!", w => w.WithTimeout(TimeSpan.FromSeconds(60))))
.WithOutputConsumer(
Consume.RedirectStdoutAndStderrToStream(
Console.OpenStandardOutput(),
Console.OpenStandardError()))
.Build();
}
public string GetServiceBusConnectionString()
=> $"Endpoint=sb://{_serviceBusContainer.Hostname}:{_serviceBusContainer.GetMappedPublicPort(SERVICE_BUS_PORT)};SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=SAS_KEY_VALUE;UseDevelopmentEmulator=true;";
public async Task InitializeAsync()
{
await Task.WhenAll(
_azureSqlEdgeContainer.StartAsync(),
_serviceBusContainer.StartAsync());
await Task.Delay(TimeSpan.FromSeconds(2));
}
public Task DisposeAsync()
=> Task.WhenAll(
_azureSqlEdgeContainer.StopAsync(),
_serviceBusContainer.StopAsync(),
_containersNetwork.DeleteAsync());
}
[CollectionDefinition(nameof(CollectionIntegrationTests))]
public sealed class CollectionIntegrationTests : ICollectionFixture<IntegrationTestsFactory> { }