Skip to content
This repository was archived by the owner on Jun 1, 2024. It is now read-only.

Commit 6c2497e

Browse files
committed
Integration tests for 6.x and refactored common bits
1 parent 81ad889 commit 6c2497e

15 files changed

+331
-146
lines changed

Serilog.Sinks.Elasticsearch.IntegrationTests/Bootstrap/ClientTestClusterBase.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ namespace Serilog.Sinks.Elasticsearch.IntegrationTests.Bootstrap
88
public abstract class ClientTestClusterBase : XunitClusterBase<ClientTestClusterConfiguration>
99
{
1010
protected ClientTestClusterBase(ClientTestClusterConfiguration configuration) : base(configuration) { }
11-
12-
public IElasticClient Client => this.GetOrAddClient(s => ConnectionSettings(s));
13-
14-
protected virtual ConnectionSettings ConnectionSettings(ConnectionSettings s) => s;
1511
}
1612

1713
public class ClientTestClusterConfiguration : XunitClusterConfiguration
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using Elasticsearch.Net;
3+
4+
namespace Serilog.Sinks.Elasticsearch.IntegrationTests.Bootstrap
5+
{
6+
public static class ElasticsearchSinkOptionsFactory
7+
{
8+
public static ElasticsearchSinkOptions Create(string indexPrefix, string templateName, Action<ElasticsearchSinkOptions> alterOptions = null)
9+
{
10+
// make sure we run through `ipv4.fiddler` if `fiddler` or `mitmproxy` is running
11+
// NOTE with the latter you need to add `ipv4.fiddler` as an alias to 127.0.0.1 in your HOSTS file manually
12+
var pool = new SingleNodeConnectionPool(new Uri($"http://{ProxyDetection.LocalOrProxyHost}:9200"));
13+
var options = new ElasticsearchSinkOptions(pool)
14+
{
15+
IndexFormat = indexPrefix + "{0:yyyy.MM.dd}",
16+
TemplateName = templateName,
17+
};
18+
19+
alterOptions?.Invoke(options);
20+
// here we make sure we set a proxy on the elasticsearch connection
21+
// when we detect `mitmproxy` running. This is a cli tool similar to fiddler
22+
// on *nix systems which aids debugging what goes over the wire
23+
var provided = options.ModifyConnectionSettings;
24+
options.ModifyConnectionSettings = configuration =>
25+
{
26+
if (ProxyDetection.RunningMitmProxy)
27+
configuration.Proxy(ProxyDetection.MitmProxyAddress, null, (string) null);
28+
configuration = provided?.Invoke(configuration) ?? configuration;
29+
return configuration;
30+
};
31+
32+
return options;
33+
}
34+
35+
}
36+
}

Serilog.Sinks.Elasticsearch.IntegrationTests/Bootstrap/EphemeralClusterExtensions.cs

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Linq;
4+
5+
namespace Serilog.Sinks.Elasticsearch.IntegrationTests.Bootstrap
6+
{
7+
public static class ProxyDetection
8+
{
9+
public static readonly bool RunningMitmProxy = Process.GetProcessesByName("mitmproxy").Any();
10+
private static readonly bool RunningFiddler = Process.GetProcessesByName("fiddler").Any();
11+
public static string LocalOrProxyHost => RunningFiddler || RunningMitmProxy ? "ipv4.fiddler" : "localhost";
12+
13+
public static readonly Uri MitmProxyAddress = new Uri("http://localhost:8080");
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Serilog.Sinks.Elasticsearch.IntegrationTests.Bootstrap;
2+
3+
namespace Serilog.Sinks.Elasticsearch.IntegrationTests.Elasticsearch6.Bootstrap
4+
{
5+
public class Elasticsearch6XCluster : ClientTestClusterBase
6+
{
7+
public Elasticsearch6XCluster() : base(CreateConfiguration()) { }
8+
9+
private static ClientTestClusterConfiguration CreateConfiguration()
10+
{
11+
return new ClientTestClusterConfiguration("6.6.0")
12+
{
13+
MaxConcurrency = 1
14+
};
15+
}
16+
17+
protected override void SeedCluster() { }
18+
}
19+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.Security.Cryptography.X509Certificates;
3+
using Elastic.Managed.Ephemeral;
4+
using Elastic.Xunit;
5+
using Elastic.Xunit.XunitPlumbing;
6+
using Elasticsearch.Net6;
7+
using Nest6;
8+
using Serilog.Sinks.Elasticsearch.IntegrationTests.Bootstrap;
9+
10+
namespace Serilog.Sinks.Elasticsearch.IntegrationTests.Elasticsearch6.Bootstrap
11+
{
12+
public abstract class Elasticsearch6XTestBase : IClusterFixture<Elasticsearch6XCluster>
13+
{
14+
protected Elasticsearch6XTestBase(Elasticsearch6XCluster cluster) => Cluster = cluster;
15+
16+
private Elasticsearch6XCluster Cluster { get; }
17+
18+
protected IElasticClient Client => this.CreateClient();
19+
20+
protected virtual ConnectionSettings SetClusterSettings(ConnectionSettings s) => s;
21+
22+
private IElasticClient CreateClient() =>
23+
Cluster.GetOrAddClient(c =>
24+
{
25+
var clusterNodes = c.NodesUris(ProxyDetection.LocalOrProxyHost);
26+
var settings = new ConnectionSettings(new StaticConnectionPool(clusterNodes));
27+
if (ProxyDetection.RunningMitmProxy)
28+
settings = settings.Proxy(new Uri("http://localhost:8080"), null, null);
29+
settings = SetClusterSettings(settings);
30+
31+
var current = (IConnectionConfigurationValues)settings;
32+
var notAlreadyAuthenticated = current.BasicAuthenticationCredentials == null && current.ClientCertificates == null;
33+
var noCertValidation = current.ServerCertificateValidationCallback == null;
34+
35+
var config = Cluster.ClusterConfiguration;
36+
if (config.EnableSecurity && notAlreadyAuthenticated)
37+
settings = settings.BasicAuthentication(ClusterAuthentication.Admin.Username, ClusterAuthentication.Admin.Password);
38+
if (config.EnableSsl && noCertValidation)
39+
{
40+
//todo use CA callback instead of allow all
41+
// ReSharper disable once UnusedVariable
42+
var ca = new X509Certificate2(config.FileSystem.CaCertificate);
43+
settings = settings.ServerCertificateValidationCallback(CertificateValidations.AllowAll);
44+
}
45+
var client = new ElasticClient(settings);
46+
return client;
47+
});
48+
}
49+
50+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System.Linq;
2+
using Elastic.Xunit.XunitPlumbing;
3+
using FluentAssertions;
4+
using Serilog.Sinks.Elasticsearch.IntegrationTests.Bootstrap;
5+
using Serilog.Sinks.Elasticsearch.IntegrationTests.Elasticsearch6.Bootstrap;
6+
using Xunit;
7+
8+
namespace Serilog.Sinks.Elasticsearch.IntegrationTests.Elasticsearch6
9+
{
10+
public class Elasticsearch6X : Elasticsearch6XTestBase, IClassFixture<Elasticsearch6X.SetupSerilog>
11+
{
12+
private readonly SetupSerilog _setup;
13+
14+
public Elasticsearch6X(Elasticsearch6XCluster cluster, SetupSerilog setup) : base(cluster) => _setup = setup;
15+
16+
17+
[I] public void AssertTemplate()
18+
{
19+
var templateResponse = Client.GetIndexTemplate(t=>t.Name(SetupSerilog.TemplateName));
20+
templateResponse.TemplateMappings.Should().NotBeEmpty();
21+
templateResponse.TemplateMappings.Keys.Should().Contain(SetupSerilog.TemplateName);
22+
23+
var template = templateResponse.TemplateMappings[SetupSerilog.TemplateName];
24+
25+
template.IndexPatterns.Should().Contain(pattern => pattern.StartsWith(SetupSerilog.IndexPrefix));
26+
}
27+
28+
[I] public void AssertLogs()
29+
{
30+
var refreshed = Client.Refresh(SetupSerilog.IndexPrefix + "*");
31+
32+
var search = Client.Search<object>(s => s
33+
.Index(SetupSerilog.IndexPrefix + "*")
34+
.Type(ElasticsearchSinkOptions.DefaultTypeName)
35+
);
36+
37+
// Informational should be filtered
38+
search.Documents.Count().Should().Be(4);
39+
}
40+
41+
// ReSharper disable once ClassNeverInstantiated.Global
42+
public class SetupSerilog
43+
{
44+
public const string IndexPrefix = "logs-6x-";
45+
public const string TemplateName = "serilog-logs-6x";
46+
47+
public SetupSerilog()
48+
{
49+
var loggerConfig = new LoggerConfiguration()
50+
.MinimumLevel.Information()
51+
.WriteTo.ColoredConsole()
52+
.WriteTo.Elasticsearch(
53+
ElasticsearchSinkOptionsFactory.Create(IndexPrefix, TemplateName, o =>
54+
{
55+
o.AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv6;
56+
o.AutoRegisterTemplate = true;
57+
})
58+
);
59+
var logger = loggerConfig.CreateLogger();
60+
61+
logger.Information("Hello Information");
62+
logger.Debug("Hello Debug");
63+
logger.Warning("Hello Warning");
64+
logger.Error("Hello Error");
65+
logger.Fatal("Hello Fatal");
66+
67+
logger.Dispose();
68+
}
69+
}
70+
}
71+
}

Serilog.Sinks.Elasticsearch.IntegrationTests/Clusters/Elasticsearch7xCluster.cs renamed to Serilog.Sinks.Elasticsearch.IntegrationTests/Elasticsearch7/Bootstrap/Elasticsearch7XCluster.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
using Serilog.Sinks.Elasticsearch.IntegrationTests.Bootstrap;
22

3-
namespace Serilog.Sinks.Elasticsearch.IntegrationTests.Clusters
3+
namespace Serilog.Sinks.Elasticsearch.IntegrationTests.Elasticsearch7.Bootstrap
44
{
5-
/// <summary>
6-
/// Use this cluster for APIs that do writes. If they are however intrusive or long running consider IntrusiveOperationCluster
7-
/// instead.
8-
/// </summary>
95
public class Elasticsearch7XCluster : ClientTestClusterBase
106
{
117
public Elasticsearch7XCluster() : base(CreateConfiguration()) { }
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Security.Cryptography.X509Certificates;
3+
using Elastic.Managed.Ephemeral;
4+
using Elastic.Xunit;
5+
using Elastic.Xunit.XunitPlumbing;
6+
using Elasticsearch.Net;
7+
using Nest;
8+
using Serilog.Sinks.Elasticsearch.IntegrationTests.Bootstrap;
9+
using Xunit;
10+
11+
namespace Serilog.Sinks.Elasticsearch.IntegrationTests.Elasticsearch7.Bootstrap
12+
{
13+
public abstract class Elasticsearch7XTestBase : IClusterFixture<Elasticsearch7XCluster>
14+
{
15+
protected Elasticsearch7XTestBase(Elasticsearch7XCluster cluster) => Cluster = cluster;
16+
17+
private Elasticsearch7XCluster Cluster { get; }
18+
19+
protected IElasticClient Client => this.CreateClient();
20+
21+
protected virtual ConnectionSettings SetClusterSettings(ConnectionSettings s) => s;
22+
23+
private IElasticClient CreateClient() =>
24+
Cluster.GetOrAddClient(c =>
25+
{
26+
var clusterNodes = c.NodesUris(ProxyDetection.LocalOrProxyHost);
27+
var settings = new ConnectionSettings(new StaticConnectionPool(clusterNodes));
28+
if (ProxyDetection.RunningMitmProxy)
29+
settings = settings.Proxy(new Uri("http://localhost:8080"), null, (string)null);
30+
settings = SetClusterSettings(settings);
31+
32+
var current = (IConnectionConfigurationValues)settings;
33+
var notAlreadyAuthenticated = current.BasicAuthenticationCredentials == null && current.ClientCertificates == null;
34+
var noCertValidation = current.ServerCertificateValidationCallback == null;
35+
36+
var config = Cluster.ClusterConfiguration;
37+
if (config.EnableSecurity && notAlreadyAuthenticated)
38+
settings = settings.BasicAuthentication(ClusterAuthentication.Admin.Username, ClusterAuthentication.Admin.Password);
39+
if (config.EnableSsl && noCertValidation)
40+
{
41+
//todo use CA callback instead of allow all
42+
// ReSharper disable once UnusedVariable
43+
var ca = new X509Certificate2(config.FileSystem.CaCertificate);
44+
settings = settings.ServerCertificateValidationCallback(CertificateValidations.AllowAll);
45+
}
46+
var client = new ElasticClient(settings);
47+
return client;
48+
});
49+
}
50+
51+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System.Linq;
2+
using Elastic.Xunit.XunitPlumbing;
3+
using FluentAssertions;
4+
using Serilog.Sinks.Elasticsearch.IntegrationTests.Bootstrap;
5+
using Serilog.Sinks.Elasticsearch.IntegrationTests.Elasticsearch7.Bootstrap;
6+
using Xunit;
7+
8+
namespace Serilog.Sinks.Elasticsearch.IntegrationTests.Elasticsearch7
9+
{
10+
public class Elasticsearch7X : Elasticsearch7XTestBase, IClassFixture<Elasticsearch7X.SetupSerilog>
11+
{
12+
private readonly SetupSerilog _setup;
13+
14+
public Elasticsearch7X(Elasticsearch7XCluster cluster, SetupSerilog setup) : base(cluster) => _setup = setup;
15+
16+
[I] public void AssertTemplate()
17+
{
18+
var templateResponse = Client.Indices.GetTemplate(SetupSerilog.TemplateName);
19+
templateResponse.TemplateMappings.Should().NotBeEmpty();
20+
templateResponse.TemplateMappings.Keys.Should().Contain(SetupSerilog.TemplateName);
21+
22+
var template = templateResponse.TemplateMappings[SetupSerilog.TemplateName];
23+
24+
template.IndexPatterns.Should().Contain(pattern => pattern.StartsWith(SetupSerilog.IndexPrefix));
25+
}
26+
27+
[I] public void AssertLogs()
28+
{
29+
var refreshed = Client.Indices.Refresh(SetupSerilog.IndexPrefix + "*");
30+
31+
var search = Client.Search<object>(s => s.Index(SetupSerilog.IndexPrefix + "*"));
32+
33+
// Informational should be filtered
34+
search.Documents.Count().Should().Be(4);
35+
}
36+
37+
// ReSharper disable once ClassNeverInstantiated.Global
38+
public class SetupSerilog
39+
{
40+
public const string IndexPrefix = "logs-7x-";
41+
public const string TemplateName = "serilog-logs-7x";
42+
43+
public SetupSerilog()
44+
{
45+
var loggerConfig = new LoggerConfiguration()
46+
.MinimumLevel.Information()
47+
.WriteTo.ColoredConsole()
48+
.WriteTo.Elasticsearch(
49+
ElasticsearchSinkOptionsFactory.Create(IndexPrefix, TemplateName, o =>
50+
{
51+
o.AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7;
52+
o.AutoRegisterTemplate = true;
53+
})
54+
);
55+
using (var logger = loggerConfig.CreateLogger())
56+
{
57+
logger.Information("Hello Information");
58+
logger.Debug("Hello Debug");
59+
logger.Warning("Hello Warning");
60+
logger.Error("Hello Error");
61+
logger.Fatal("Hello Fatal");
62+
}
63+
}
64+
}
65+
66+
}
67+
}

0 commit comments

Comments
 (0)