Skip to content

Commit 5b99ac7

Browse files
authored
Merge pull request #83 from amccool/testcontainer
Move to Testcontainer.dotnet
2 parents f3b44dc + 41139ac commit 5b99ac7

File tree

12 files changed

+230
-104
lines changed

12 files changed

+230
-104
lines changed

example/SampleApp/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ public Program()
3939
.AddEventSourceLogger()
4040
.AddElasticSearch(options =>
4141
{
42-
//options.ElasticsearchEndpoint = new Uri(@"http://localhost:9200/");
43-
options.ElasticsearchEndpoint = new Uri(@"http://es.devint.dev-r5ead.net:9200/");
42+
options.ElasticsearchEndpoint = new Uri(@"http://localhost:9200/");
4443
});
4544
});
4645

src/ElasticLogger.Test/APMTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using ElasticLogger.Test.Factory;
2+
using ElasticLogger.Test.Fixture;
3+
using System.Threading.Tasks;
4+
using Xunit;
5+
using Xunit.Abstractions;
6+
7+
namespace ElasticLogger.Test
8+
{
9+
public class APMTests : IClassFixture<ElasticsearchFixture>
10+
{
11+
private readonly ElasticsearchFixture _fixture;
12+
private readonly ITestOutputHelper _output;
13+
14+
public APMTests(ElasticsearchFixture fixture, ITestOutputHelper outputHelper)
15+
{
16+
_fixture = fixture;
17+
_output = outputHelper;
18+
}
19+
20+
[Fact]
21+
public async Task LoggingThing()
22+
{
23+
await _fixture.ReadyAsync();
24+
25+
var factory = new CustomWebApplicationFactory<Startup>(_fixture);
26+
27+
var client = factory.CreateClient();
28+
29+
await client.GetAsync("/");
30+
}
31+
}
32+
}

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,11 +7,11 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="elasticsearch-inside" Version="6.6.1" />
1110
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
1211
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="3.1.3" />
1312
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.0" />
1413
<PackageReference Include="NEST" Version="6.0.2" />
14+
<PackageReference Include="Testcontainers" Version="2.2.0" />
1515
<PackageReference Include="xunit" Version="2.4.1" />
1616
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
1717
<PrivateAssets>all</PrivateAssets>

src/ElasticLogger.Test/ElasticsearchLoggerFilterTests.cs

Lines changed: 30 additions & 18 deletions
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,20 +10,29 @@
1010
using Nest;
1111
using Xunit.Abstractions;
1212
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
13+
using ElasticLogger.Test.Entities;
14+
using ElasticLogger.Test.Fixture;
1315

1416
namespace ElasticLogger.Test
1517
{
16-
public class ElasticsearchLoggerFilterTests : IClassFixture<ESFixture>
18+
public class ElasticsearchLoggerFilterTests : IClassFixture<ElasticsearchFixture>
1719
{
1820
private readonly ITestOutputHelper _output;
19-
private readonly ESFixture _fixture;
21+
private readonly ElasticsearchFixture _fixture;
2022

21-
public ElasticsearchLoggerFilterTests(ESFixture fixture, ITestOutputHelper output)
23+
public ElasticsearchLoggerFilterTests(ElasticsearchFixture fixture, ITestOutputHelper output)
2224
{
2325
_fixture = fixture;
2426
_output = output;
2527
}
2628

29+
private ConnectionSettings connectionSettings(Uri endpoint)
30+
{
31+
var connSettings = new ConnectionSettings(endpoint);
32+
connSettings.ServerCertificateValidationCallback((obj, cert, chain, policyerrors) => true);
33+
return connSettings;
34+
}
35+
2736
[Fact]
2837
public async Task No_categories_for_es_should_use_default()
2938
{
@@ -62,7 +71,7 @@ public async Task No_categories_for_es_should_use_default()
6271
logger.LogTrace("bananas taste yucky");
6372

6473
var delayTask = Task.Delay(TimeSpan.FromSeconds(5));
65-
var client = new ElasticClient(new ConnectionSettings(_fixture.Endpoint));
74+
var client = new ElasticClient(connectionSettings(_fixture.Endpoint));
6675
await client.PingAsync();
6776
await delayTask;
6877

@@ -123,7 +132,7 @@ public async Task Configured_categories_dont_log_for_config()
123132
logger.LogTrace("bananas taste yucky");
124133

125134
var delayTask = Task.Delay(TimeSpan.FromSeconds(5));
126-
var client = new ElasticClient(new ConnectionSettings(_fixture.Endpoint));
135+
var client = new ElasticClient(connectionSettings(_fixture.Endpoint));
127136
await client.PingAsync();
128137
await delayTask;
129138

@@ -179,7 +188,7 @@ public async Task No_elasticsearch_section_should_use_default_log_level()
179188
logger.LogTrace("bananas taste yucky");
180189

181190
var delayTask = Task.Delay(TimeSpan.FromSeconds(5));
182-
var client = new ElasticClient(new ConnectionSettings(_fixture.Endpoint));
191+
var client = new ElasticClient(connectionSettings(_fixture.Endpoint));
183192
await client.PingAsync();
184193
await delayTask;
185194

@@ -285,7 +294,7 @@ private async Task LevelTesterPositive(string source, LogLevel logLevel)
285294
logger.Log(logLevel, new EventId(), circularRefObj, null, (circle, exception) => "");
286295

287296
var delayTask = Task.Delay(TimeSpan.FromSeconds(5));
288-
var client = new ElasticClient(new ConnectionSettings(_fixture.Endpoint));
297+
var client = new ElasticClient(connectionSettings(_fixture.Endpoint));
289298
await client.PingAsync();
290299
await delayTask;
291300

@@ -335,7 +344,7 @@ private async Task LevelTesterNegative(string source, LogLevel logLevel)
335344
logger.Log(logLevel, new EventId(), circularRefObj, null, (circle, exception) => "");
336345

337346
var delayTask = Task.Delay(TimeSpan.FromSeconds(5));
338-
var client = new ElasticClient(new ConnectionSettings(_fixture.Endpoint));
347+
var client = new ElasticClient(connectionSettings(_fixture.Endpoint));
339348
await client.PingAsync();
340349
await delayTask;
341350

@@ -357,7 +366,7 @@ private async Task LevelTesterNegative(string source, LogLevel logLevel)
357366
public async Task Load_ES_with_explicit_type_write_and_search()
358367
{
359368
await _fixture.ReadyAsync();
360-
var client = new ElasticClient(new ConnectionSettings(_fixture.Endpoint));
369+
var client = new ElasticClient(connectionSettings(_fixture.Endpoint));
361370
await client.PingAsync();
362371

363372
var tweet = new Tweet
@@ -418,7 +427,7 @@ public async Task Missing_ElasticSearch_Section_In_Config_Should_Use_Logging_Def
418427
logger.LogTrace("bananas taste yucky");
419428

420429
var delayTask = Task.Delay(TimeSpan.FromSeconds(5));
421-
var client = new ElasticClient(new ConnectionSettings(_fixture.Endpoint));
430+
var client = new ElasticClient(connectionSettings(_fixture.Endpoint));
422431
await client.PingAsync();
423432
await delayTask;
424433

@@ -470,7 +479,8 @@ public async Task Missing_ElasticSearch_Section_In_Config_Should_Use_Logging_Def
470479
logger.LogError("bananas taste yucky");
471480

472481
var delayTask = Task.Delay(TimeSpan.FromSeconds(5));
473-
var client = new ElasticClient(new ConnectionSettings(_fixture.Endpoint));
482+
483+
var client = new ElasticClient(connectionSettings(_fixture.Endpoint));
474484
await client.PingAsync();
475485
await delayTask;
476486

@@ -487,6 +497,8 @@ public async Task Missing_ElasticSearch_Section_In_Config_Should_Use_Logging_Def
487497
Assert.Single(dyndocs.Documents);
488498
}
489499

500+
501+
490502
[Fact]
491503
public async Task Existing_ElasticSearch_Section_In_Config_Should_Be_Used_For_Log_Levels_Positive_Match()
492504
{
@@ -523,7 +535,7 @@ public async Task Existing_ElasticSearch_Section_In_Config_Should_Be_Used_For_Lo
523535
logger.LogTrace("bananas taste yucky");
524536

525537
var delayTask = Task.Delay(TimeSpan.FromSeconds(5));
526-
var client = new ElasticClient(new ConnectionSettings(_fixture.Endpoint));
538+
var client = new ElasticClient(connectionSettings(_fixture.Endpoint));
527539
await client.PingAsync();
528540
await delayTask;
529541

@@ -576,7 +588,7 @@ public async Task Existing_ElasticSearch_Section_In_Config_Should_Be_Used_For_Lo
576588
logger.LogCritical("bananas taste yucky");
577589

578590
var delayTask = Task.Delay(TimeSpan.FromSeconds(5));
579-
var client = new ElasticClient(new ConnectionSettings(_fixture.Endpoint));
591+
var client = new ElasticClient(connectionSettings(_fixture.Endpoint));
580592
await client.PingAsync();
581593
await delayTask;
582594

@@ -630,7 +642,7 @@ public async Task Existing_ElasticSearch_Section_With_Category_Should_Match_Top_
630642
logger.LogInformation("bananas taste yucky");
631643

632644
var delayTask = Task.Delay(TimeSpan.FromSeconds(5));
633-
var client = new ElasticClient(new ConnectionSettings(_fixture.Endpoint));
645+
var client = new ElasticClient(connectionSettings(_fixture.Endpoint));
634646
await client.PingAsync();
635647
await delayTask;
636648

@@ -684,7 +696,7 @@ public async Task Existing_ElasticSearch_Section_With_Category_Should_Match_Top_
684696
logger.LogInformation("bananas taste yucky");
685697

686698
var delayTask = Task.Delay(TimeSpan.FromSeconds(5));
687-
var client = new ElasticClient(new ConnectionSettings(_fixture.Endpoint));
699+
var client = new ElasticClient(connectionSettings(_fixture.Endpoint));
688700
await client.PingAsync();
689701
await delayTask;
690702

@@ -738,7 +750,7 @@ public async Task Existing_ElasticSearch_Section_With_Category_Should_Match_Bott
738750
logger.LogCritical("bananas taste yucky");
739751

740752
var delayTask = Task.Delay(TimeSpan.FromSeconds(5));
741-
var client = new ElasticClient(new ConnectionSettings(_fixture.Endpoint));
753+
var client = new ElasticClient(connectionSettings(_fixture.Endpoint));
742754
await client.PingAsync();
743755
await delayTask;
744756

@@ -792,7 +804,7 @@ public async Task Existing_ElasticSearch_Section_With_Category_Should_Match_Bott
792804
logger.LogInformation("bananas taste yucky");
793805

794806
var delayTask = Task.Delay(TimeSpan.FromSeconds(5));
795-
var client = new ElasticClient(new ConnectionSettings(_fixture.Endpoint));
807+
var client = new ElasticClient(connectionSettings(_fixture.Endpoint));
796808
await client.PingAsync();
797809
await delayTask;
798810

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
}

src/ElasticLogger.Test/CustomWebApplicationFactory.cs renamed to src/ElasticLogger.Test/Factory/CustomWebApplicationFactory.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Text;
5-
using AM.Extensions.Logging.ElasticSearch;
1+
using AM.Extensions.Logging.ElasticSearch;
2+
using ElasticLogger.Test.Fixture;
63
using Microsoft.AspNetCore;
74
using Microsoft.AspNetCore.Hosting;
85
using Microsoft.AspNetCore.Mvc.Testing;
96
using Microsoft.Extensions.Configuration;
10-
using Microsoft.Extensions.DependencyInjection;
11-
using Microsoft.Extensions.Logging;
7+
using System;
8+
using System.IO;
129

13-
namespace ElasticLogger.Test
10+
namespace ElasticLogger.Test.Factory
1411
{
15-
public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup: class
12+
public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
1613
{
14+
private ElasticsearchFixture _fixture;
15+
16+
public CustomWebApplicationFactory(ElasticsearchFixture fixture) : base()
17+
{
18+
_fixture = fixture;
19+
}
20+
21+
1722
protected override IWebHostBuilder CreateWebHostBuilder()
1823
{
1924
return WebHost.CreateDefaultBuilder()
@@ -32,7 +37,8 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
3237

3338
builder.ConfigureLogging((hostingContext, logging) =>
3439
{
35-
Uri uri = new Uri(@"http://es.fakething.net:9200");
40+
//Uri uri = new Uri(@"http://es.fakething.net:9200");
41+
Uri uri = _fixture.Endpoint;
3642

3743
logging.AddElasticSearch(options => { options.ElasticsearchEndpoint = uri; });
3844
});

src/ElasticLogger.Test/Startup.cs renamed to src/ElasticLogger.Test/Factory/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Extensions.Logging;
1010

11-
namespace ElasticLogger.Test
11+
namespace ElasticLogger.Test.Factory
1212
{
1313
class Startup
1414
{

0 commit comments

Comments
 (0)