Skip to content

Commit ced971d

Browse files
author
Alex McCool
committed
create an initial test using Elasticsearch testcontainer.dotnet
1 parent efe5907 commit ced971d

File tree

6 files changed

+169
-135
lines changed

6 files changed

+169
-135
lines changed
Lines changed: 78 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,97 @@
11
using DotNet.Testcontainers.Builders;
22
using DotNet.Testcontainers.Configurations;
33
using DotNet.Testcontainers.Containers;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.Logging;
46
using System.Threading.Tasks;
57
using Xunit;
68

79
namespace ElasticLogger.Test
810
{
9-
internal class ContainerESTests
11+
public class ContainerESTests
1012
{
11-
ContainerESTests()
12-
{
13-
}
14-
13+
[Fact]
1514
public async Task DoTheTest()
1615
{
17-
await new TestcontainersBuilder<ElasticsearchTestcontainer>()
18-
.WithDatabase(new ElasticsearchTestcontainerConfiguration())
19-
.Build()
20-
.StartAsync()
21-
.ConfigureAwait(false);
22-
}
23-
}
16+
var serviceCollection = new ServiceCollection();
17+
serviceCollection.AddLogging(b => {
18+
b.AddDebug();
19+
b.AddConsole();
20+
});
2421

22+
var secureHttpServiceProvider = serviceCollection.BuildServiceProvider();
23+
var loggerFactory = secureHttpServiceProvider.GetService<ILoggerFactory>();
2524

26-
//[Collection(nameof(Testcontainers))]
27-
public sealed class ElasticsearchTestcontainerTest : IClassFixture<ElasticsearchFixture>
28-
{
29-
private readonly ElasticsearchFixture elasticsearchFixture;
25+
ILogger logger = loggerFactory.CreateLogger("es");
3026

31-
public ElasticsearchTestcontainerTest(ElasticsearchFixture elasticsearchFixture)
32-
{
33-
this.elasticsearchFixture = elasticsearchFixture;
34-
}
27+
TestcontainersSettings.Logger = logger;
3528

36-
[Fact]
37-
[Trait("Category", "Elasticsearch")]
38-
public async Task ConnectionEstablished()
39-
{
40-
// Given
41-
var connection = this.elasticsearchFixture.Connection;
4229

43-
// When
44-
var result = await connection.InfoAsync()
45-
.ConfigureAwait(false);
30+
/**
31+
* WARNING
32+
* "org.elasticsearch.plugins.PluginsService","elasticsearch.node.name":"1b1454ac59a8","elasticsearch.cluster.name":"docker-cluster"}
33+
* bootstrap check failure [1] of [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
34+
* ERROR: Elasticsearch did not exit normally - check the logs at /usr/share/elasticsearch/logs/docker-cluster.log
35+
*
36+
* ERROR: [1] bootstrap checks failed. You must address the points described in the following [1] lines before starting Elasticsearch.
37+
*
38+
*
39+
*
40+
* in docker-desktop
41+
* https://gist.github.com/jarek-przygodzki/4721932bba3bd434512ae6cc58f508b0
42+
* wsl -d docker-desktop
43+
* echo "vm.max_map_count = 262144" > /etc/sysctl.d/99-docker-desktop.conf
44+
* reboot
45+
*
46+
* basically the linux VM running docker needs
47+
* wsl -d docker-desktop
48+
* sysctl -w vm.max_map_count=262144
49+
*
50+
*
51+
**/
52+
53+
var container = new TestcontainersBuilder<ElasticsearchTestcontainer>()
54+
.WithDatabase(new ElasticsearchTestcontainerConfiguration()
55+
{
56+
Password="aaaa",
57+
58+
})
59+
//.WithAutoRemove(false)
60+
//.WithWaitStrategy( Wait.ForUnixContainer())
61+
//.WithOutputConsumer(Consume.RedirectStdoutAndStderrToConsole())
62+
.Build();
63+
64+
await container.StartAsync()
65+
.ConfigureAwait(false);
4666

47-
// Then
48-
Assert.True(result.IsValid);
67+
Assert.NotEmpty(container.ConnectionString);
4968
}
5069
}
70+
71+
72+
////[Collection(nameof(Testcontainers))]
73+
//public sealed class ElasticsearchTestcontainerTest : IClassFixture<ElasticsearchFixture>
74+
//{
75+
// private readonly ElasticsearchFixture elasticsearchFixture;
76+
77+
// public ElasticsearchTestcontainerTest(ElasticsearchFixture elasticsearchFixture)
78+
// {
79+
// this.elasticsearchFixture = elasticsearchFixture;
80+
// }
81+
82+
// [Fact]
83+
// [Trait("Category", "Elasticsearch")]
84+
// public async Task ConnectionEstablished()
85+
// {
86+
// // Given
87+
// var connection = this.elasticsearchFixture.Connection;
88+
89+
// // When
90+
// var result = await connection.InfoAsync()
91+
// .ConfigureAwait(false);
92+
93+
// // Then
94+
// Assert.True(result.IsValid);
95+
// }
96+
//}
97+
}

src/ElasticLogger.Test/ESFixture.cs

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/ElasticLogger.Test/ElasticLogger.Test.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<!--<PackageReference Include="elasticsearch-inside" Version="6.6.1" />-->
10+
<PackageReference Include="elasticsearch-inside" Version="6.6.1" />
1111
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
1212
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.3" />
1313
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.0" />

src/ElasticLogger.Test/ElasticsearchLoggerFilterTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Xunit.Abstractions;
1212
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
1313
using ElasticLogger.Test.Entities;
14+
using ElasticLogger.Test.Fixture;
1415

1516
namespace ElasticLogger.Test
1617
{
Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,50 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
2+
using System.Threading.Tasks;
3+
using Xunit;
4+
using Xunit.Abstractions;
45

56
namespace ElasticLogger.Test.Fixture
67
{
7-
internal class ESFixture
8+
public class ESFixture : IAsyncLifetime, IDisposable
89
{
9-
}
10+
//private ITestOutputHelper _output;
11+
private readonly ElasticsearchInside.Elasticsearch _elasticsearch;
1012

11-
namespace DotNet.Testcontainers.Tests.Fixtures
12-
{
13-
using System;
14-
using System.Threading.Tasks;
15-
using DotNet.Testcontainers.Builders;
16-
using DotNet.Testcontainers.Configurations;
17-
using DotNet.Testcontainers.Containers;
18-
using Elastic.Clients.Elasticsearch;
19-
using Elastic.Transport;
20-
using Elasticsearch.Net;
21-
using global::DotNet.Testcontainers.Builders;
22-
using global::DotNet.Testcontainers.Configurations;
23-
using global::DotNet.Testcontainers.Containers;
24-
using JetBrains.Annotations;
25-
26-
[UsedImplicitly]
27-
public sealed class ElasticsearchFixture : DatabaseFixture<ElasticsearchTestcontainer, ElasticsearchClient>
13+
public Uri Endpoint => _elasticsearch.Url;
14+
15+
private readonly IMessageSink _messageSink;
16+
17+
public ESFixture(IMessageSink messageSink)
18+
{
19+
_messageSink = messageSink;
20+
21+
_elasticsearch = new ElasticsearchInside.Elasticsearch(c => c.SetElasticsearchStartTimeout(60)
22+
.EnableLogging()
23+
.LogTo(s => _messageSink.OnMessage(new Xunit.Sdk.DiagnosticMessage(s ?? string.Empty)))
24+
);
25+
26+
_elasticsearch.ReadySync();
27+
}
28+
29+
public Task<ElasticsearchInside.Elasticsearch> ReadyAsync()
30+
{
31+
return _elasticsearch.Ready();
32+
}
33+
34+
public void Dispose()
35+
{
36+
//uber importadante !
37+
_elasticsearch?.Dispose();
38+
}
39+
40+
public Task InitializeAsync()
41+
{
42+
return Task.CompletedTask;
43+
}
44+
45+
public Task DisposeAsync()
2846
{
29-
private readonly TestcontainerDatabaseConfiguration configuration = new ElasticsearchTestcontainerConfiguration { Password = "secret" };
30-
31-
public ElasticsearchFixture()
32-
{
33-
this.Container = new TestcontainersBuilder<ElasticsearchTestcontainer>()
34-
.WithDatabase(this.configuration)
35-
.Build();
36-
}
37-
38-
public override async Task InitializeAsync()
39-
{
40-
await this.Container.StartAsync()
41-
.ConfigureAwait(false);
42-
43-
var settings = new ElasticsearchClientSettings(new Uri(this.Container.ConnectionString))
44-
.ServerCertificateValidationCallback(CertificateValidations.AllowAll)
45-
.Authentication(new BasicAuthentication(this.Container.Username, this.Container.Password));
46-
47-
this.Connection = new ElasticsearchClient(settings);
48-
}
49-
50-
public override async Task DisposeAsync()
51-
{
52-
await this.Container.DisposeAsync()
53-
.ConfigureAwait(false);
54-
}
55-
56-
public override void Dispose()
57-
{
58-
this.configuration.Dispose();
59-
}
47+
return Task.CompletedTask;
6048
}
6149
}
62-
}
50+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//using DotNet.Testcontainers.Builders;
2+
//using DotNet.Testcontainers.Configurations;
3+
//using DotNet.Testcontainers.Containers;
4+
//using Elasticsearch.Net;
5+
//using System;
6+
//using System.Collections.Generic;
7+
//using System.Linq;
8+
//using System.Text;
9+
//using System.Threading.Tasks;
10+
11+
//namespace ElasticLogger.Test.Fixture
12+
//{
13+
// [UsedImplicitly]
14+
// public sealed class ElasticsearchFixture : DatabaseFixture<ElasticsearchTestcontainer, ElasticsearchClient>
15+
// {
16+
// private readonly TestcontainerDatabaseConfiguration configuration = new ElasticsearchTestcontainerConfiguration { Password = "secret" };
17+
18+
// public ElasticsearchFixture()
19+
// {
20+
// this.Container = new TestcontainersBuilder<ElasticsearchTestcontainer>()
21+
// .WithDatabase(this.configuration)
22+
// .Build();
23+
// }
24+
25+
// public override async Task InitializeAsync()
26+
// {
27+
// await this.Container.StartAsync()
28+
// .ConfigureAwait(false);
29+
30+
// var settings = new ElasticsearchClientSettings(new Uri(this.Container.ConnectionString))
31+
// .ServerCertificateValidationCallback(CertificateValidations.AllowAll)
32+
// .Authentication(new BasicAuthentication(this.Container.Username, this.Container.Password));
33+
34+
// this.Connection = new ElasticsearchClient(settings);
35+
// }
36+
37+
// public override async Task DisposeAsync()
38+
// {
39+
// await this.Container.DisposeAsync()
40+
// .ConfigureAwait(false);
41+
// }
42+
43+
// public override void Dispose()
44+
// {
45+
// this.configuration.Dispose();
46+
// }
47+
// }
48+
//}

0 commit comments

Comments
 (0)