Skip to content

Commit 7b761da

Browse files
author
Alex McCool
committed
prepare to move to testcontainers
1 parent c648be7 commit 7b761da

File tree

8 files changed

+125
-10
lines changed

8 files changed

+125
-10
lines changed

example/SampleApp/SampleApp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
66
</PropertyGroup>
77

88
<ItemGroup>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using DotNet.Testcontainers.Builders;
2+
using DotNet.Testcontainers.Configurations;
3+
using DotNet.Testcontainers.Containers;
4+
using System.Threading.Tasks;
5+
using Xunit;
6+
7+
namespace ElasticLogger.Test
8+
{
9+
internal class ContainerESTests
10+
{
11+
ContainerESTests()
12+
{
13+
}
14+
15+
public async Task DoTheTest()
16+
{
17+
await new TestcontainersBuilder<ElasticsearchTestcontainer>()
18+
.WithDatabase(new ElasticsearchTestcontainerConfiguration())
19+
.Build()
20+
.StartAsync()
21+
.ConfigureAwait(false);
22+
}
23+
}
24+
25+
26+
//[Collection(nameof(Testcontainers))]
27+
public sealed class ElasticsearchTestcontainerTest : IClassFixture<ElasticsearchFixture>
28+
{
29+
private readonly ElasticsearchFixture elasticsearchFixture;
30+
31+
public ElasticsearchTestcontainerTest(ElasticsearchFixture elasticsearchFixture)
32+
{
33+
this.elasticsearchFixture = elasticsearchFixture;
34+
}
35+
36+
[Fact]
37+
[Trait("Category", "Elasticsearch")]
38+
public async Task ConnectionEstablished()
39+
{
40+
// Given
41+
var connection = this.elasticsearchFixture.Connection;
42+
43+
// When
44+
var result = await connection.InfoAsync()
45+
.ConfigureAwait(false);
46+
47+
// Then
48+
Assert.True(result.IsValid);
49+
}
50+
}

src/ElasticLogger.Test/ElasticLogger.Test.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
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" />
1414
<PackageReference Include="NEST" Version="6.0.2" />
15+
<PackageReference Include="Testcontainers" Version="2.2.0" />
1516
<PackageReference Include="xunit" Version="2.4.1" />
1617
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
1718
<PrivateAssets>all</PrivateAssets>

src/ElasticLogger.Test/ElasticsearchLoggerFilterTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
 using Microsoft.Extensions.Configuration;
1+
using Microsoft.Extensions.Configuration;
22
using Microsoft.Extensions.DependencyInjection;
33
using Microsoft.Extensions.Logging;
44
using AM.Extensions.Logging.ElasticSearch;
@@ -10,6 +10,7 @@
1010
using Nest;
1111
using Xunit.Abstractions;
1212
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
13+
using ElasticLogger.Test.Entities;
1314

1415
namespace ElasticLogger.Test
1516
{

src/ElasticLogger.Test/Circle.cs renamed to src/ElasticLogger.Test/Entities/Circle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace ElasticLogger.Test
1+
namespace ElasticLogger.Test.Entities
22
{
33
public class Circle
44
{

src/ElasticLogger.Test/Tweet.cs renamed to src/ElasticLogger.Test/Entities/Tweet.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace ElasticLogger.Test
3+
namespace ElasticLogger.Test.Entities
44
{
55
internal class Tweet : IEquatable<Tweet>
66
{
@@ -15,18 +15,18 @@ public override bool Equals(object obj)
1515
{
1616
if (ReferenceEquals(null, obj)) return false;
1717
if (ReferenceEquals(this, obj)) return true;
18-
if (obj.GetType() != this.GetType()) return false;
19-
return Equals((Tweet) obj);
18+
if (obj.GetType() != GetType()) return false;
19+
return Equals((Tweet)obj);
2020
}
2121

2222
public override int GetHashCode()
2323
{
2424
unchecked
2525
{
2626
var hashCode = Id;
27-
hashCode = (hashCode * 397) ^ (User != null ? User.GetHashCode() : 0);
28-
hashCode = (hashCode * 397) ^ PostDate.GetHashCode();
29-
hashCode = (hashCode * 397) ^ (Message != null ? Message.GetHashCode() : 0);
27+
hashCode = hashCode * 397 ^ (User != null ? User.GetHashCode() : 0);
28+
hashCode = hashCode * 397 ^ PostDate.GetHashCode();
29+
hashCode = hashCode * 397 ^ (Message != null ? Message.GetHashCode() : 0);
3030
return hashCode;
3131
}
3232
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace ElasticLogger.Test.Fixture
6+
{
7+
internal class ESFixture
8+
{
9+
}
10+
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>
28+
{
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+
}
60+
}
61+
}
62+
}

src/ElasticLogger.Test/MiscTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.IO;
22
using System.Threading.Tasks;
33
using AM.Extensions.Logging.ElasticSearch;
4+
using ElasticLogger.Test.Entities;
45
using Newtonsoft.Json;
56
using Xunit;
67
using Xunit.Abstractions;

0 commit comments

Comments
 (0)