-
Notifications
You must be signed in to change notification settings - Fork 864
Expand file tree
/
Copy pathConsulContainerFixture.cs
More file actions
44 lines (32 loc) · 1.16 KB
/
Copy pathConsulContainerFixture.cs
File metadata and controls
44 lines (32 loc) · 1.16 KB
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
using Testcontainers.Consul;
namespace HealthChecks.Consul.Tests;
public class ConsulContainerFixture : IAsyncLifetime
{
private const string Registry = "docker.io";
private const string Image = "library/consul";
private const string Tag = "1.15.4";
public ConsulContainer? Container { get; private set; }
public ConsulOptions GetConnectionOptions()
{
if (Container is null)
{
throw new InvalidOperationException("The test container was not initialized.");
}
return new ConsulOptions
{
HostName = Container.Hostname,
Port = Container.GetMappedPublicPort(ConsulBuilder.ConsulHttpPort),
RequireHttps = false
};
}
public async Task InitializeAsync() => Container = await CreateContainerAsync();
public Task DisposeAsync() => Container?.DisposeAsync().AsTask() ?? Task.CompletedTask;
private async Task<ConsulContainer?> CreateContainerAsync()
{
var container = new ConsulBuilder()
.WithImage($"{Registry}/{Image}:{Tag}")
.Build();
await container.StartAsync();
return container;
}
}