-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic Elasticsearch transport tests (#85)
- Loading branch information
Showing
6 changed files
with
187 additions
and
0 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
57 changes: 57 additions & 0 deletions
57
tests/Elastic.Elasticsearch.IntegrationTests/DefaultCluster.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,57 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using Elastic.Elasticsearch.Ephemeral; | ||
using Elastic.Elasticsearch.Xunit; | ||
using Elastic.Transport; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
using static Elastic.Elasticsearch.Ephemeral.ClusterAuthentication; | ||
|
||
[assembly: TestFramework("Elastic.Elasticsearch.Xunit.Sdk.ElasticTestFramework", "Elastic.Elasticsearch.Xunit")] | ||
|
||
namespace Elastic.Elasticsearch.IntegrationTests; | ||
|
||
/// <summary> Declare our cluster that we want to inject into our test classes </summary> | ||
public class DefaultCluster : XunitClusterBase | ||
{ | ||
protected static string Version = "8.7.0"; | ||
public DefaultCluster() : this(new XunitClusterConfiguration(Version) { StartingPortNumber = 9202 }) { } | ||
public DefaultCluster(XunitClusterConfiguration xunitClusterConfiguration) : base(xunitClusterConfiguration) { } | ||
|
||
public DefaultHttpTransport CreateClient(ITestOutputHelper output) => | ||
this.GetOrAddClient(_ => | ||
{ | ||
var hostName = (System.Diagnostics.Process.GetProcessesByName("mitmproxy").Any() | ||
? "ipv4.fiddler" | ||
: "localhost"); | ||
var nodes = NodesUris(hostName); | ||
var connectionPool = new StaticNodePool(nodes); | ||
var settings = new TransportConfiguration(connectionPool) | ||
.Proxy(new Uri("http://ipv4.fiddler:8080"), null!, null!) | ||
.RequestTimeout(TimeSpan.FromSeconds(5)) | ||
.ServerCertificateValidationCallback(CertificateValidations.AllowAll) | ||
.OnRequestCompleted(d => | ||
{ | ||
try { output.WriteLine(d.DebugInformation);} | ||
catch | ||
{ | ||
// ignored | ||
} | ||
}) | ||
.EnableDebugMode(); | ||
if (ClusterConfiguration.Features.HasFlag(ClusterFeatures.Security)) | ||
settings = settings.Authentication(new BasicAuthentication(Admin.Username, Admin.Password)); | ||
return new DefaultHttpTransport(settings); | ||
}); | ||
} | ||
|
||
public class SecurityCluster : DefaultCluster | ||
{ | ||
public SecurityCluster() : base(new XunitClusterConfiguration(Version, ClusterFeatures.Security | ClusterFeatures.SSL | ClusterFeatures.XPack) | ||
{ | ||
StartingPortNumber = 9202 | ||
//, TrialMode = XPackTrialMode.Trial | ||
}) { } | ||
} |
32 changes: 32 additions & 0 deletions
32
tests/Elastic.Elasticsearch.IntegrationTests/DefaultClusterTests.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,32 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using Elastic.Transport; | ||
using FluentAssertions; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
using static Elastic.Transport.HttpMethod; | ||
|
||
namespace Elastic.Elasticsearch.IntegrationTests; | ||
|
||
public class DefaultClusterTests : IntegrationTestBase | ||
{ | ||
public DefaultClusterTests(DefaultCluster cluster, ITestOutputHelper output) : base(cluster, output) { } | ||
|
||
[Fact] | ||
public async Task AsyncRequestDoesNotThrow() | ||
{ | ||
var response = await Transport.RequestAsync<StringResponse>(GET, "/"); | ||
response.ApiCallDetails.Should().NotBeNull(); | ||
response.ApiCallDetails.HasSuccessfulStatusCode.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void SyncRequestDoesNotThrow() | ||
{ | ||
var response = Transport.Request<StringResponse>(GET, "/"); | ||
response.ApiCallDetails.Should().NotBeNull(); | ||
response.ApiCallDetails.HasSuccessfulStatusCode.Should().BeTrue(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
tests/Elastic.Elasticsearch.IntegrationTests/Elastic.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,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Elastic.Elasticsearch.Xunit" Version="0.4.1" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="FluentAssertions" Version="5.10.3" /> | ||
<PackageReference Include="Nullean.VsTest.Pretty.TestLogger" Version="0.3.0" /> | ||
<PackageReference Include="JunitXml.TestLogger" Version="2.1.81" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Elastic.Transport\Elastic.Transport.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
27 changes: 27 additions & 0 deletions
27
tests/Elastic.Elasticsearch.IntegrationTests/IntegrationTestBase.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,27 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using Elastic.Elasticsearch.Xunit.XunitPlumbing; | ||
using Elastic.Transport; | ||
using Xunit.Abstractions; | ||
|
||
namespace Elastic.Elasticsearch.IntegrationTests; | ||
|
||
public abstract class IntegrationTestBase : IntegrationTestBase<DefaultCluster> | ||
{ | ||
protected IntegrationTestBase(DefaultCluster cluster, ITestOutputHelper output) : base(cluster, output) { } | ||
} | ||
public abstract class IntegrationTestBase<TCluster> : IClusterFixture<TCluster> | ||
where TCluster : DefaultCluster, new() | ||
{ | ||
protected TCluster Cluster { get; } | ||
protected DefaultHttpTransport Transport { get; } | ||
|
||
|
||
protected IntegrationTestBase(TCluster cluster, ITestOutputHelper output) | ||
{ | ||
Cluster = cluster; | ||
Transport = cluster.CreateClient(output); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
tests/Elastic.Elasticsearch.IntegrationTests/SecurityClusterTests.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,32 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using Elastic.Transport; | ||
using FluentAssertions; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
using static Elastic.Transport.HttpMethod; | ||
|
||
namespace Elastic.Elasticsearch.IntegrationTests; | ||
|
||
public class SecurityClusterTests : IntegrationTestBase<SecurityCluster> | ||
{ | ||
public SecurityClusterTests(SecurityCluster cluster, ITestOutputHelper output) : base(cluster, output) { } | ||
|
||
[Fact] | ||
public async Task AsyncRequestDoesNotThrow() | ||
{ | ||
var response = await Transport.RequestAsync<StringResponse>(GET, "/"); | ||
response.ApiCallDetails.Should().NotBeNull(); | ||
response.ApiCallDetails.HasSuccessfulStatusCode.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void SyncRequestDoesNotThrow() | ||
{ | ||
var response = Transport.Request<StringResponse>(GET, "/"); | ||
response.ApiCallDetails.Should().NotBeNull(); | ||
response.ApiCallDetails.HasSuccessfulStatusCode.Should().BeTrue(); | ||
} | ||
} |