-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRabbitMqCollection.cs
80 lines (64 loc) · 2.07 KB
/
RabbitMqCollection.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Containers;
using IntegrationTests.Helpers;
using static IntegrationTests.Helpers.DockerFileHelper;
namespace IntegrationTests;
[CollectionDefinition(Name)]
public class RabbitMqCollection : ICollectionFixture<RabbitMqFixture>
{
public const string Name = nameof(RabbitMqCollection);
}
public class RabbitMqFixture : IAsyncLifetime
{
private const int RabbitMqPort = 5672;
private static readonly string RabbitMqImage = ReadImageFrom("rabbitmq.Dockerfile");
private IContainer? _container;
public RabbitMqFixture()
{
if (IsCurrentArchitectureSupported)
{
Port = TcpPortProvider.GetOpenPort();
}
}
public bool IsCurrentArchitectureSupported { get; } = EnvironmentTools.IsX64();
public int Port { get; }
public async Task InitializeAsync()
{
if (!IsCurrentArchitectureSupported)
{
return;
}
_container = await LaunchRabbitMqContainerAsync(Port);
}
public async Task DisposeAsync()
{
if (_container != null)
{
await ShutdownRabbitMqContainerAsync(_container);
}
}
public void SkipIfUnsupportedPlatform()
{
if (!IsCurrentArchitectureSupported)
{
throw new SkipException("RabbitMQ is supported only on AMD64.");
}
}
private static async Task<IContainer> LaunchRabbitMqContainerAsync(int port)
{
var containersBuilder = new ContainerBuilder()
.WithImage(RabbitMqImage)
.WithName($"rabbitmq-{port}")
.WithPortBinding(port, RabbitMqPort)
.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(RabbitMqPort));
var container = containersBuilder.Build();
await container.StartAsync();
return container;
}
private static async Task ShutdownRabbitMqContainerAsync(IContainer container)
{
await container.DisposeAsync();
}
}