-
Notifications
You must be signed in to change notification settings - Fork 864
Expand file tree
/
Copy pathElasticContainerFixture.cs
More file actions
83 lines (70 loc) · 2.88 KB
/
Copy pathElasticContainerFixture.cs
File metadata and controls
83 lines (70 loc) · 2.88 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
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.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using Ductus.FluentDocker.Builders;
using Ductus.FluentDocker.Services;
using Ductus.FluentDocker.Services.Extensions;
namespace HealthChecks.Elasticsearch.Tests.Fixtures;
public class ElasticContainerFixture : IAsyncLifetime
{
private const string SETUP_DONE_MESSAGE = "All done!";
private const long TIME_OUT_IN_MILLIS = 180000;
private const string ELASTIC_CONTAINER_NAME = "es01";
private const string CONTAINER_CERTIFICATE_PATH = "/usr/share/elasticsearch/config/certs/ca/ca.crt";
public const string ELASTIC_PASSWORD = "abcDEF123!";
private readonly string _composeFilePath = $"{Directory.GetCurrentDirectory()}/Resources/docker-compose.yml";
private readonly ICompositeService _compositeService;
public string? ApiKey { get; set; }
public ElasticContainerFixture()
{
_compositeService = new Builder()
.UseContainer()
.UseCompose()
.FromFile(_composeFilePath)
.ForceRecreate()
.Build()
.Start();
var elasticContainer =
_compositeService.Containers.First(container => container.Name.Contains(ELASTIC_CONTAINER_NAME));
var setupContainer = _compositeService.Containers.First(container => container != elasticContainer);
setupContainer.WaitForMessageInLogs(SETUP_DONE_MESSAGE, TIME_OUT_IN_MILLIS);
elasticContainer.CopyFrom(CONTAINER_CERTIFICATE_PATH, ".", true);
}
public async Task InitializeAsync() => ApiKey = await SetApiKeyInElasticSearchAsync().ConfigureAwait(false);
public Task DisposeAsync()
{
Dispose();
return Task.CompletedTask;
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_compositeService.Dispose();
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private async Task<string> SetApiKeyInElasticSearchAsync()
{
var handler = new HttpClientHandler
{
ServerCertificateCustomValidationCallback = delegate
{
return true;
}
};
using var httpClient = new HttpClient(handler);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(
Encoding.ASCII.GetBytes($"elastic:{ELASTIC_PASSWORD}")));
using var response = await httpClient.PostAsJsonAsync("https://localhost:9200/_security/api_key?pretty",
new { name = "new-api-key", role_descriptors = new { } }).ConfigureAwait(false);
var apiKeyResponse = await response.Content.ReadFromJsonAsync<ApiKeyResponse>().ConfigureAwait(false) ?? throw new JsonException();
return apiKeyResponse.Encoded;
}
private record ApiKeyResponse(string Encoded);
}