This repository was archived by the owner on Jun 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Started on new integration tests project
- Loading branch information
Showing
20 changed files
with
391 additions
and
359 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,7 @@ root=true | |
|
||
[*] | ||
indent_style = space | ||
indent_size = 4 | ||
indent_size = 4 | ||
|
||
[*.csproj] | ||
indent_size = 2 |
41 changes: 41 additions & 0 deletions
41
Serilog.Sinks.Elasticsearch.IntegrationTests/Bootstrap/ClientTestClusterBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using Elastic.Managed.Ephemeral; | ||
using Elastic.Managed.Ephemeral.Plugins; | ||
using Elastic.Xunit; | ||
using Nest; | ||
|
||
namespace Serilog.Sinks.Elasticsearch.IntegrationTests.Bootstrap | ||
{ | ||
public abstract class ClientTestClusterBase : XunitClusterBase<ClientTestClusterConfiguration> | ||
{ | ||
protected ClientTestClusterBase(ClientTestClusterConfiguration configuration) : base(configuration) { } | ||
|
||
public IElasticClient Client => this.GetOrAddClient(s => ConnectionSettings(s)); | ||
|
||
protected virtual ConnectionSettings ConnectionSettings(ConnectionSettings s) => s; | ||
} | ||
|
||
public class ClientTestClusterConfiguration : XunitClusterConfiguration | ||
{ | ||
public ClientTestClusterConfiguration( | ||
string elasticsearchVersion, | ||
ClusterFeatures features = ClusterFeatures.None, | ||
int numberOfNodes = 1, | ||
params ElasticsearchPlugin[] plugins | ||
) | ||
: base(elasticsearchVersion, features, new ElasticsearchPlugins(plugins), numberOfNodes) | ||
{ | ||
HttpFiddlerAware = true; | ||
CacheEsHomeInstallation = true; | ||
|
||
Add(AttributeKey("testingcluster"), "true"); | ||
|
||
Add($"script.max_compilations_per_minute", "10000", "<6.0.0-rc1"); | ||
Add($"script.max_compilations_rate", "10000/1m", ">=6.0.0-rc1"); | ||
|
||
Add($"script.inline", "true", "<5.5.0"); | ||
Add($"script.stored", "true", ">5.0.0-alpha1 <5.5.0"); | ||
Add($"script.indexed", "true", "<5.0.0-alpha1"); | ||
Add($"script.allowed_types", "inline,stored", ">=5.5.0"); | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
Serilog.Sinks.Elasticsearch.IntegrationTests/Bootstrap/EphemeralClusterExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Security.Cryptography.X509Certificates; | ||
using Elastic.Managed.Ephemeral; | ||
using Elastic.Xunit; | ||
using Elasticsearch.Net; | ||
using Nest; | ||
|
||
namespace Serilog.Sinks.Elasticsearch.IntegrationTests.Bootstrap | ||
{ | ||
public static class EphemeralClusterExtensions | ||
{ | ||
private static readonly bool RunningMitmProxy = Process.GetProcessesByName("mitmproxy").Any(); | ||
private static readonly bool RunningFiddler = Process.GetProcessesByName("fiddler").Any(); | ||
private static string LocalOrProxyHost => RunningFiddler || RunningMitmProxy ? "ipv4.fiddler" : "localhost"; | ||
|
||
public static ConnectionSettings CreateConnectionSettings<TConfig>(this IEphemeralCluster<TConfig> cluster) | ||
where TConfig : EphemeralClusterConfiguration | ||
{ | ||
var clusterNodes = cluster.NodesUris(LocalOrProxyHost); | ||
return new ConnectionSettings(new StaticConnectionPool(clusterNodes)); | ||
} | ||
|
||
public static IElasticClient GetOrAddClient<TConfig>( | ||
this IEphemeralCluster<TConfig> cluster, | ||
Func<ConnectionSettings, ConnectionSettings> modifySettings = null | ||
) | ||
where TConfig : EphemeralClusterConfiguration | ||
{ | ||
modifySettings = modifySettings ?? (s => s); | ||
return cluster.GetOrAddClient(c => | ||
{ | ||
var settings = modifySettings(cluster.CreateConnectionSettings()); | ||
|
||
var current = (IConnectionConfigurationValues)settings; | ||
var notAlreadyAuthenticated = current.BasicAuthenticationCredentials == null && current.ClientCertificates == null; | ||
var noCertValidation = current.ServerCertificateValidationCallback == null; | ||
|
||
if (cluster.ClusterConfiguration.EnableSecurity && notAlreadyAuthenticated) | ||
settings = settings.BasicAuthentication(ClusterAuthentication.Admin.Username, ClusterAuthentication.Admin.Password); | ||
if (cluster.ClusterConfiguration.EnableSsl && noCertValidation) | ||
{ | ||
//todo use CA callback instead of allow all | ||
// ReSharper disable once UnusedVariable | ||
var ca = new X509Certificate2(cluster.ClusterConfiguration.FileSystem.CaCertificate); | ||
settings = settings.ServerCertificateValidationCallback(CertificateValidations.AllowAll); | ||
} | ||
var client = new ElasticClient(settings); | ||
return client; | ||
}); | ||
} | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
...Sinks.Elasticsearch.IntegrationTests/Bootstrap/SerilogSinkElasticsearchXunitRunOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Text; | ||
using Elastic.Xunit; | ||
|
||
namespace Serilog.Sinks.Elasticsearch.IntegrationTests.Bootstrap | ||
{ | ||
/// <summary> Feeding TestClient.Configuration options to the runner</summary> | ||
public class SerilogSinkElasticsearchXunitRunOptions : ElasticXunitRunOptions | ||
{ | ||
public SerilogSinkElasticsearchXunitRunOptions() | ||
{ | ||
RunIntegrationTests = true; | ||
RunUnitTests = false; | ||
ClusterFilter = null; | ||
TestFilter = null; | ||
IntegrationTestsMayUseAlreadyRunningNode = false; | ||
} | ||
|
||
public override void OnBeforeTestsRun() { } | ||
|
||
public override void OnTestsFinished(Dictionary<string, Stopwatch> clusterTotals, ConcurrentBag<Tuple<string, string>> failedCollections) | ||
{ | ||
Console.Out.Flush(); | ||
DumpClusterTotals(clusterTotals); | ||
DumpFailedCollections(failedCollections); | ||
} | ||
|
||
private static void DumpClusterTotals(Dictionary<string, Stopwatch> clusterTotals) | ||
{ | ||
Console.WriteLine("--------"); | ||
Console.WriteLine("Individual cluster running times:"); | ||
foreach (var kv in clusterTotals) Console.WriteLine($"- {kv.Key}: {kv.Value.Elapsed}"); | ||
Console.WriteLine("--------"); | ||
} | ||
|
||
private static void DumpFailedCollections(ConcurrentBag<Tuple<string, string>> failedCollections) | ||
{ | ||
if (failedCollections.Count <= 0) return; | ||
|
||
Console.ForegroundColor = ConsoleColor.Red; | ||
Console.WriteLine("Failed collections:"); | ||
foreach (var t in failedCollections.OrderBy(p => p.Item1).ThenBy(t => t.Item2)) | ||
|
||
{ | ||
var cluster = t.Item1; | ||
Console.WriteLine($" - {cluster}: {t.Item2}"); | ||
} | ||
DumpReproduceFilters(failedCollections); | ||
Console.ResetColor(); | ||
} | ||
|
||
private static void DumpReproduceFilters(ConcurrentBag<Tuple<string, string>> failedCollections) | ||
{ | ||
Console.ForegroundColor = ConsoleColor.Yellow; | ||
Console.WriteLine("---Reproduce: -----"); | ||
var reproduceLine = ReproduceCommandLine(failedCollections); | ||
Console.WriteLine(reproduceLine); | ||
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TEAMCITY_VERSION"))) | ||
Console.WriteLine($"##teamcity[buildProblem description='{reproduceLine}']"); | ||
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TF_BUILD"))) | ||
{ | ||
var count = failedCollections.Count; | ||
Console.WriteLine($"##vso[task.logissue type=error;]{count} test failures"); | ||
Console.WriteLine($"##vso[task.logissue type=error;]{reproduceLine}"); | ||
} | ||
Console.WriteLine("--------"); | ||
} | ||
|
||
private static string ReproduceCommandLine(ConcurrentBag<Tuple<string, string>> failedCollections) | ||
{ | ||
var sb = new StringBuilder("build.bat "); | ||
|
||
if (failedCollections.Count > 0) | ||
{ | ||
var clusters = string.Join(",", failedCollections | ||
.Select(c => c.Item1.ToLowerInvariant()) | ||
.Distinct()); | ||
sb.Append(" \""); | ||
sb.Append(clusters); | ||
sb.Append("\""); | ||
} | ||
|
||
if ((failedCollections.Count < 30) && failedCollections.Count > 0) | ||
{ | ||
sb.Append(" \""); | ||
var tests = string.Join(",", failedCollections | ||
.OrderBy(t => t.Item2) | ||
.Select(c => c.Item2.ToLowerInvariant() | ||
.Split('.') | ||
.Last() | ||
.Replace("apitests", "") | ||
.Replace("usagetests", "") | ||
.Replace("tests", "") | ||
)); | ||
sb.Append(tests); | ||
sb.Append("\""); | ||
} | ||
|
||
var reproduceLine = sb.ToString(); | ||
return reproduceLine; | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
Serilog.Sinks.Elasticsearch.IntegrationTests/Bootstrap/XunitBootstrap.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
using Elastic.Xunit; | ||
using Serilog.Sinks.Elasticsearch.IntegrationTests.Bootstrap; | ||
using Xunit; | ||
|
||
[assembly: TestFramework("Elastic.Xunit.Sdk.ElasticTestFramework", "Elastic.Xunit")] | ||
[assembly: ElasticXunitConfiguration(typeof(SerilogSinkElasticsearchXunitRunOptions))] |
23 changes: 23 additions & 0 deletions
23
Serilog.Sinks.Elasticsearch.IntegrationTests/Clusters/Elasticsearch7xCluster.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Serilog.Sinks.Elasticsearch.IntegrationTests.Bootstrap; | ||
|
||
namespace Serilog.Sinks.Elasticsearch.IntegrationTests.Clusters | ||
{ | ||
/// <summary> | ||
/// Use this cluster for APIs that do writes. If they are however intrusive or long running consider IntrusiveOperationCluster | ||
/// instead. | ||
/// </summary> | ||
public class Elasticsearch7XCluster : ClientTestClusterBase | ||
{ | ||
public Elasticsearch7XCluster() : base(CreateConfiguration()) { } | ||
|
||
private static ClientTestClusterConfiguration CreateConfiguration() | ||
{ | ||
return new ClientTestClusterConfiguration("7.0.0") | ||
{ | ||
MaxConcurrency = 1 | ||
}; | ||
} | ||
|
||
protected override void SeedCluster() { } | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
Serilog.Sinks.Elasticsearch.IntegrationTests/Elasticsearch7X.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System.Linq; | ||
using Elastic.Managed.Ephemeral; | ||
using Elastic.Xunit; | ||
using Elastic.Xunit.XunitPlumbing; | ||
using FluentAssertions; | ||
using Nest; | ||
using Serilog.Core; | ||
using Serilog.Sinks.Elasticsearch.IntegrationTests.Clusters; | ||
using Xunit; | ||
|
||
namespace Serilog.Sinks.Elasticsearch.IntegrationTests | ||
{ | ||
public class Elasticsearch7XSetup | ||
{ | ||
public const string IndexPrefix = "logs-7x-"; | ||
public const string TemplateName = "serilog-logs-7x"; | ||
public Elasticsearch7XSetup() | ||
{ | ||
var loggerConfig = new LoggerConfiguration() | ||
.MinimumLevel.Information() | ||
.WriteTo.ColoredConsole() | ||
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions | ||
{ | ||
|
||
IndexFormat = IndexPrefix + "{0:yyyy.MM.dd}", | ||
TemplateName = TemplateName, | ||
AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7, | ||
AutoRegisterTemplate = true | ||
}); | ||
var logger = loggerConfig.CreateLogger(); | ||
|
||
logger.Information("Hello Information"); | ||
logger.Debug("Hello Debug"); | ||
logger.Warning("Hello Warning"); | ||
logger.Error("Hello Error"); | ||
logger.Fatal("Hello Fatal"); | ||
|
||
logger.Dispose(); | ||
} | ||
|
||
} | ||
|
||
|
||
public class Elasticsearch7X : IClusterFixture<Elasticsearch7XCluster>, IClassFixture<Elasticsearch7XSetup> | ||
{ | ||
private readonly Elasticsearch7XCluster _cluster; | ||
private IElasticClient _client; | ||
|
||
public Elasticsearch7X(Elasticsearch7XCluster cluster, Elasticsearch7XSetup setup) | ||
{ | ||
_cluster = cluster; | ||
_client = cluster.Client; | ||
} | ||
|
||
|
||
[I] public void AssertTemplate() | ||
{ | ||
var templateResponse = _client.Indices.GetTemplate(Elasticsearch7XSetup.TemplateName); | ||
templateResponse.TemplateMappings.Should().NotBeEmpty(); | ||
templateResponse.TemplateMappings.Keys.Should().Contain(Elasticsearch7XSetup.TemplateName); | ||
|
||
var template = templateResponse.TemplateMappings[Elasticsearch7XSetup.TemplateName]; | ||
|
||
template.IndexPatterns.Should().Contain(pattern => pattern.StartsWith(Elasticsearch7XSetup.IndexPrefix)); | ||
|
||
} | ||
[I] public void AssertLogs() | ||
{ | ||
var refreshed = _client.Indices.Refresh(Elasticsearch7XSetup.IndexPrefix + "*"); | ||
|
||
var search = _client.Search<object>(s => s.Index(Elasticsearch7XSetup.IndexPrefix + "*")); | ||
|
||
// Informational should be filtered | ||
search.Documents.Count().Should().Be(4); | ||
|
||
|
||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
....Sinks.Elasticsearch.IntegrationTests/Serilog.Sinks.Elasticsearch.IntegrationTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFrameworks>netcoreapp2.1;net461</TargetFrameworks> | ||
<NoWarn>$(NoWarn);xUnit1013</NoWarn> | ||
<DebugSymbols>True</DebugSymbols> | ||
<LangVersion>latest</LangVersion> | ||
<IsTestProject>True</IsTestProject> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\src\Serilog.Sinks.Elasticsearch\Serilog.Sinks.Elasticsearch.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" /> | ||
|
||
<PackageReference Include="xunit" Version="2.3.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" /> | ||
<PackageReference Include="xunit.extensibility.execution" Version="2.3.1" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.1.1" /> | ||
<PackageReference Include="Elastic.Managed" Version="0.1.0-ci20190627T121416" /> | ||
<PackageReference Include="Elastic.Xunit" Version="0.1.0-ci20190627T121416" /> | ||
<PackageReference Include="FluentAssertions" Version="5.7.0" /> | ||
<PackageReference Include="NEST" Version="7.0.0" /> | ||
<PackageReference Include="Serilog.Sinks.ColoredConsole" Version="3.0.1" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Elasticsearch/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Serilog/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Xunit/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.