From 064be701f2e28d8e29b6e089372c5999317c020d Mon Sep 17 00:00:00 2001 From: Khalid Abuhakmeh Date: Mon, 2 Oct 2023 14:06:02 -0400 Subject: [PATCH 1/4] Add Elasticsearch example to docs. --- docs/modules/elasticsearch.md | 59 +++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 docs/modules/elasticsearch.md diff --git a/docs/modules/elasticsearch.md b/docs/modules/elasticsearch.md new file mode 100644 index 000000000..7106d4d51 --- /dev/null +++ b/docs/modules/elasticsearch.md @@ -0,0 +1,59 @@ +# Elasticsearch + +[Elasticsearch](https://www.elastic.co/elasticsearch/) is a distributed, RESTful search and analytics engine capable of addressing a growing number of use cases. As the heart of the Elastic Stack, it centrally stores data for lightning fast search, fine‑tuned relevancy, and powerful analytics that scale with ease. + +The following example uses the following NuGet packages: + +```console title="Install the NuGet dependencies" +dotnet add package Testcontainers.Elasticsearch +dotnet add package Elastic.Clients.Elasticsearch +dotnet add package xunit +``` + +IDEs and editors may also require the following packages to run tests: `xunit.runner.visualstudio` and `Microsoft.NET.Test.Sdk`. + +Copy and paste the following code into a new `.cs` test file within an existing test project. + +```csharp +using Elastic.Clients.Elasticsearch; +using Elastic.Transport; +using Testcontainers.Elasticsearch; +using Xunit; + +namespace TestcontainersModules; + +public class ElasticsearchContainerTest : IAsyncLifetime +{ + private readonly ElasticsearchContainer elasticsearch = + new ElasticsearchBuilder() + .Build(); + + [Fact] + public async Task ReadFromElasticsearch() + { + var connectionString = elasticsearch.GetConnectionString(); + var settings = new ElasticsearchClientSettings(new Uri(connectionString)); + settings.ServerCertificateValidationCallback(CertificateValidations.AllowAll); + + var client = new ElasticsearchClient(settings); + + var stats = await client.PingAsync(); + + Assert.True(stats.IsValidResponse); + } + + public Task InitializeAsync() + => elasticsearch.StartAsync(); + + public Task DisposeAsync() => + elasticsearch.DisposeAsync().AsTask(); +} +``` + +To execute the tests, use the command `dotnet test` from a terminal. + +## A Note To Developers + +The Testcontainers package creates a container that listens to requests over **HTTPS**. To communicate with the Elasticsearch instance, developers must create a `ElasticsearchClientSettings` instance and set the `ServerCertificateValidationCallback` delegate to `CertificateValidations.AllowAll`. Failing to do so will result in a communication failure as the .NET will reject the certificate coming from the container. + +[xunit]: https://xunit.net/ From 91d077550662ab0bc9cd8accdaade7fd569075c9 Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Tue, 3 Oct 2023 09:29:25 +0200 Subject: [PATCH 2/4] Update docs/modules/elasticsearch.md --- docs/modules/elasticsearch.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/modules/elasticsearch.md b/docs/modules/elasticsearch.md index 7106d4d51..3f88f18b9 100644 --- a/docs/modules/elasticsearch.md +++ b/docs/modules/elasticsearch.md @@ -24,8 +24,8 @@ namespace TestcontainersModules; public class ElasticsearchContainerTest : IAsyncLifetime { - private readonly ElasticsearchContainer elasticsearch = - new ElasticsearchBuilder() + private readonly ElasticsearchContainer elasticsearch + = new ElasticsearchBuilder() .Build(); [Fact] From 8ef5a443b63bcafcd69792dee434e1b6b96fc369 Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Tue, 3 Oct 2023 09:29:31 +0200 Subject: [PATCH 3/4] Update docs/modules/elasticsearch.md --- docs/modules/elasticsearch.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/modules/elasticsearch.md b/docs/modules/elasticsearch.md index 3f88f18b9..6fb2427ed 100644 --- a/docs/modules/elasticsearch.md +++ b/docs/modules/elasticsearch.md @@ -45,8 +45,8 @@ public class ElasticsearchContainerTest : IAsyncLifetime public Task InitializeAsync() => elasticsearch.StartAsync(); - public Task DisposeAsync() => - elasticsearch.DisposeAsync().AsTask(); + public Task DisposeAsync() + => elasticsearch.DisposeAsync().AsTask(); } ``` From f110b990c355ae7a70561c52572eb541ff18b0f2 Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Tue, 3 Oct 2023 09:29:37 +0200 Subject: [PATCH 4/4] Update docs/modules/elasticsearch.md --- docs/modules/elasticsearch.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/modules/elasticsearch.md b/docs/modules/elasticsearch.md index 6fb2427ed..00fa7ebe2 100644 --- a/docs/modules/elasticsearch.md +++ b/docs/modules/elasticsearch.md @@ -54,6 +54,6 @@ To execute the tests, use the command `dotnet test` from a terminal. ## A Note To Developers -The Testcontainers package creates a container that listens to requests over **HTTPS**. To communicate with the Elasticsearch instance, developers must create a `ElasticsearchClientSettings` instance and set the `ServerCertificateValidationCallback` delegate to `CertificateValidations.AllowAll`. Failing to do so will result in a communication failure as the .NET will reject the certificate coming from the container. +The Testcontainers module creates a container that listens to requests over **HTTPS**. To communicate with the Elasticsearch instance, developers must create a `ElasticsearchClientSettings` instance and set the `ServerCertificateValidationCallback` delegate to `CertificateValidations.AllowAll`. Failing to do so will result in a communication failure as the .NET will reject the certificate coming from the container. [xunit]: https://xunit.net/