Skip to content

add OpenSearch container and tests #1395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ jobs:
{ name: "Testcontainers.MySql", runs-on: "ubuntu-22.04" },
{ name: "Testcontainers.Nats", runs-on: "ubuntu-22.04" },
{ name: "Testcontainers.Neo4j", runs-on: "ubuntu-22.04" },
{ name: "Testcontainers.OpenSearch", runs-on: "ubuntu-22.04" },
{ name: "Testcontainers.Oracle", runs-on: "ubuntu-22.04" },
{ name: "Testcontainers.Oracle11", runs-on: "ubuntu-22.04" },
{ name: "Testcontainers.Oracle18", runs-on: "ubuntu-22.04" },
Expand Down
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<PackageVersion Include="NATS.Client" Version="1.0.8"/>
<PackageVersion Include="Neo4j.Driver" Version="5.5.0"/>
<PackageVersion Include="Npgsql" Version="6.0.11"/>
<PackageVersion Include="OpenSearch.Client" Version="1.8.0"/>
<PackageVersion Include="Oracle.ManagedDataAccess.Core" Version="23.7.0"/>
<PackageVersion Include="RabbitMQ.Client" Version="6.4.0"/>
<PackageVersion Include="RavenDB.Client" Version="5.4.100"/>
Expand Down
6 changes: 6 additions & 0 deletions Testcontainers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.WebDriver.Te
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.Xunit.Tests", "tests\Testcontainers.Xunit.Tests\Testcontainers.Xunit.Tests.csproj", "{E901DF14-6F05-4FC2-825A-3055FAD33561}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.OpenSearch", "src\Testcontainers.OpenSearch\Testcontainers.OpenSearch.csproj", "{49051DBC-6B80-4412-8505-BC2764A877BD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testcontainers.OpenSearch.Tests", "tests\Testcontainers.OpenSearch.Tests\Testcontainers.OpenSearch.Tests.csproj", "{04A7AF65-2E02-4E20-8056-2AAC0705B0BC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -794,5 +798,7 @@ Global
{DDB41BC8-5826-4D97-9C5F-001151E3FFD6} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{EBA72C3B-57D5-43FF-A5B4-3D55B3B6D4C2} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{E901DF14-6F05-4FC2-825A-3055FAD33561} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
{49051DBC-6B80-4412-8505-BC2764A877BD} = {673F23AE-7694-4BB9-ABD4-136D6C13634E}
{04A7AF65-2E02-4E20-8056-2AAC0705B0BC} = {7164F1FB-7F24-444A-ACD2-2C329C2B3CCF}
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions src/Testcontainers.OpenSearch/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
96 changes: 96 additions & 0 deletions src/Testcontainers.OpenSearch/OpenSearchBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
namespace Testcontainers.OpenSearch;

/// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" />
[PublicAPI]
public sealed class OpenSearchBuilder : ContainerBuilder<OpenSearchBuilder, OpenSearchContainer, OpenSearchConfiguration>
{
public const string DefaultUsername = "admin";

public const string DefaultPassword = "VeryStrongP@ssw0rd!";

public const string OpenSearchImage = "opensearchproject/opensearch:2.12.0";

public const ushort OpenSearchApiPort = 9200;

public const ushort OpenSearchPerfAnalyzerPort = 9600;

/// <summary>
/// Initializes a new instance of the <see cref="OpenSearchBuilder" /> class.
/// </summary>
public OpenSearchBuilder()
: this(new OpenSearchConfiguration())
{
DockerResourceConfiguration = Init().DockerResourceConfiguration;
}

/// <summary>
/// Initializes a new instance of the <see cref="OpenSearchBuilder" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
private OpenSearchBuilder(OpenSearchConfiguration resourceConfiguration)
: base(resourceConfiguration)
{
DockerResourceConfiguration = resourceConfiguration;
}

// /// <inheritdoc />
protected override OpenSearchConfiguration DockerResourceConfiguration { get; }

/// <summary>
/// Sets the password for 'admin' user.
/// </summary>
/// <param name="password">Password requires a minimum of 8 characters and must contain at least one uppercase letter, one lowercase letter, one digit, and one special character.</param>
/// <returns>A configured instance of <see cref="OpenSearchBuilder" />.</returns>
public OpenSearchBuilder WithPassword(string password)
{
return Merge(DockerResourceConfiguration, new OpenSearchConfiguration(password: password))
.WithEnvironment("OPENSEARCH_INITIAL_ADMIN_PASSWORD", password);
}

/// <inheritdoc />
public override OpenSearchContainer Build()
{
Validate();
return new OpenSearchContainer(DockerResourceConfiguration);
}

/// <inheritdoc />
protected override OpenSearchBuilder Init()
{
return base.Init()
.WithImage(OpenSearchImage)
.WithPortBinding(OpenSearchApiPort, true)
.WithPortBinding(OpenSearchPerfAnalyzerPort, true)
.WithPassword(DefaultPassword)
.WithEnvironment("discovery.type", "single-node")
.WithWaitStrategy(Wait.ForUnixContainer().UntilMessageIsLogged("Node '.*' initialized"));
}

/// <inheritdoc />
protected override void Validate()
{
base.Validate();

_ = Guard.Argument(DockerResourceConfiguration.Password, nameof(DockerResourceConfiguration.Password))
.NotNull()
.NotEmpty();
}

/// <inheritdoc />
protected override OpenSearchBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new OpenSearchConfiguration(resourceConfiguration));
}

/// <inheritdoc />
protected override OpenSearchBuilder Clone(IContainerConfiguration resourceConfiguration)
{
return Merge(DockerResourceConfiguration, new OpenSearchConfiguration(resourceConfiguration));
}

/// <inheritdoc />
protected override OpenSearchBuilder Merge(OpenSearchConfiguration oldValue, OpenSearchConfiguration newValue)
{
return new OpenSearchBuilder(new OpenSearchConfiguration(oldValue, newValue));
}
}
61 changes: 61 additions & 0 deletions src/Testcontainers.OpenSearch/OpenSearchConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
namespace Testcontainers.OpenSearch;

/// <inheritdoc cref="ContainerConfiguration" />
[PublicAPI]
public sealed class OpenSearchConfiguration : ContainerConfiguration
{
/// <summary>
/// Initializes a new instance of the <see cref="OpenSearchConfiguration" /> class.
/// </summary>
/// <param name="password">Password for default user 'admin'.</param>
public OpenSearchConfiguration(string password = null)
{
Password = password;
}

/// <summary>
/// Initializes a new instance of the <see cref="OpenSearchConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public OpenSearchConfiguration(IResourceConfiguration<CreateContainerParameters> resourceConfiguration)
: base(resourceConfiguration)
{

}

/// <summary>
/// Initializes a new instance of the <see cref="OpenSearchConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public OpenSearchConfiguration(IContainerConfiguration resourceConfiguration)
: base(resourceConfiguration)
{

}

/// <summary>
/// Initializes a new instance of the <see cref="OpenSearchConfiguration" /> class.
/// </summary>
/// <param name="resourceConfiguration">The Docker resource configuration.</param>
public OpenSearchConfiguration(OpenSearchConfiguration resourceConfiguration)
: this(new OpenSearchConfiguration(), resourceConfiguration)
{

}

/// <summary>
/// Initializes a new instance of the <see cref="OpenSearchConfiguration" /> class.
/// </summary>
/// <param name="oldValue">The old Docker resource configuration.</param>
/// <param name="newValue">The new Docker resource configuration.</param>
public OpenSearchConfiguration(OpenSearchConfiguration oldValue, OpenSearchConfiguration newValue)
: base(oldValue, newValue)
{
Password = BuildConfiguration.Combine(oldValue.Password, newValue.Password);
}

/// <summary>
/// Gets the password for default user 'admin'.
/// </summary>
public string Password { get; }
}
28 changes: 28 additions & 0 deletions src/Testcontainers.OpenSearch/OpenSearchContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Testcontainers.OpenSearch;

/// <inheritdoc cref="DockerContainer" />
[PublicAPI]
public sealed class OpenSearchContainer : DockerContainer
{
/// <summary>
/// Initializes a new instance of the <see cref="OpenSearchContainer" /> class.
/// </summary>
/// <param name="configuration">The container configuration.</param>
public OpenSearchContainer(OpenSearchConfiguration configuration)
: base(configuration)
{
}

/// <summary>
/// Returns the url which is used to connect to OpenSearch cluster.
/// </summary>
/// <returns></returns>
public string GetConnection()
{
return new UriBuilder(
Uri.UriSchemeHttps,
Hostname,
GetMappedPublicPort(OpenSearchBuilder.OpenSearchApiPort)
).ToString();
}
}
12 changes: 12 additions & 0 deletions src/Testcontainers.OpenSearch/Testcontainers.OpenSearch.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0;net9.0;netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" VersionOverride="2023.3.0" PrivateAssets="All"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../Testcontainers/Testcontainers.csproj"/>
</ItemGroup>
</Project>
7 changes: 7 additions & 0 deletions src/Testcontainers.OpenSearch/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
global using System;
global using Docker.DotNet.Models;
global using DotNet.Testcontainers;
global using DotNet.Testcontainers.Builders;
global using DotNet.Testcontainers.Configurations;
global using DotNet.Testcontainers.Containers;
global using JetBrains.Annotations;
1 change: 1 addition & 0 deletions tests/Testcontainers.OpenSearch.Tests/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
root = true
34 changes: 34 additions & 0 deletions tests/Testcontainers.OpenSearch.Tests/OpenSearchContainerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace Testcontainers.OpenSearch;

public sealed class OpenSearchContainerTest : IAsyncLifetime
{
private readonly OpenSearchContainer _opensearchContainer = new OpenSearchBuilder().Build();

public Task InitializeAsync()
{
return _opensearchContainer.StartAsync();
}

public Task DisposeAsync()
{
return _opensearchContainer.DisposeAsync().AsTask();
}

[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
public void PingReturnsValidResponse()
{
// Given
var clientSettings = new ConnectionSettings(new Uri(_opensearchContainer.GetConnection()))
.BasicAuthentication(OpenSearchBuilder.DefaultUsername, OpenSearchBuilder.DefaultPassword)
.ServerCertificateValidationCallback((_, _, _, _) => true); // self-signed certificate

var client = new OpenSearchClient(clientSettings);

// When
var response = client.Ping();

// Then
Assert.True(response.IsValid);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net9.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<IsPublishable>false</IsPublishable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk"/>
<PackageReference Include="coverlet.collector"/>
<PackageReference Include="xunit.runner.visualstudio"/>
<PackageReference Include="xunit"/>
<PackageReference Include="OpenSearch.Client"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../../src/Testcontainers.OpenSearch/Testcontainers.OpenSearch.csproj"/>
<ProjectReference Include="../Testcontainers.Commons/Testcontainers.Commons.csproj"/>
</ItemGroup>
</Project>
5 changes: 5 additions & 0 deletions tests/Testcontainers.OpenSearch.Tests/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
global using OpenSearch.Client;
global using System;
global using System.Threading.Tasks;
global using DotNet.Testcontainers.Commons;
global using Xunit;