-
Notifications
You must be signed in to change notification settings - Fork 864
Expand file tree
/
Copy pathK3sContainerFixture.cs
More file actions
45 lines (31 loc) · 1.25 KB
/
Copy pathK3sContainerFixture.cs
File metadata and controls
45 lines (31 loc) · 1.25 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
45
using System.Text;
using k8s;
using Testcontainers.K3s;
namespace HealthChecks.Kubernetes.Tests;
public class K3sContainerFixture : IAsyncLifetime
{
private const string Registry = "docker.io";
private const string Image = "rancher/k3s";
private const string Tag = "v1.26.2-k3s1";
public K3sContainer? Container { get; private set; }
public async Task<KubernetesClientConfiguration> GetKubeconfigAsync()
{
if (Container is null)
{
throw new InvalidOperationException("The test container was not initialized.");
}
string? kubeconfig = await Container.GetKubeconfigAsync();
await using var stream = new MemoryStream(Encoding.UTF8.GetBytes(kubeconfig));
return await KubernetesClientConfiguration.BuildConfigFromConfigFileAsync(stream);
}
public async Task InitializeAsync() => Container = await CreateContainerAsync();
public Task DisposeAsync() => Container?.DisposeAsync().AsTask() ?? Task.CompletedTask;
private static async Task<K3sContainer> CreateContainerAsync()
{
var container = new K3sBuilder()
.WithImage($"{Registry}/{Image}:{Tag}")
.Build();
await container.StartAsync();
return container;
}
}