Skip to content

Commit 725d613

Browse files
committed
chore: Align tests
1 parent 65a5dd8 commit 725d613

File tree

5 files changed

+162
-161
lines changed

5 files changed

+162
-161
lines changed

docs/modules/opensearch.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,40 @@ This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle
2323
--8<-- "tests/Testcontainers.OpenSearch.Tests/OpenSearchContainerTest.cs:BaseClass"
2424
}
2525
```
26-
=== "SslBasicAuth"
26+
=== "Insecure no auth"
2727
```csharp
28-
--8<-- "tests/Testcontainers.OpenSearch.Tests/OpenSearchContainerTest.cs:SslBasicAuth"
28+
--8<-- "tests/Testcontainers.OpenSearch.Tests/OpenSearchContainerTest.cs:InsecureNoAuth"
2929
```
30-
=== "SslBasicAuth CustomPassword"
30+
=== "SSL default credentials"
3131
```csharp
32-
--8<-- "tests/Testcontainers.OpenSearch.Tests/OpenSearchContainerTest.cs:SslBasicAuthCustomPassword"
32+
--8<-- "tests/Testcontainers.OpenSearch.Tests/OpenSearchContainerTest.cs:SslBasicAuthDefaultCredentials"
3333
```
34-
=== "InsecureNoAuth"
34+
=== "SSL custom credentials"
3535
```csharp
36-
--8<-- "tests/Testcontainers.OpenSearch.Tests/OpenSearchContainerTest.cs:InsecureNoAuth"
36+
--8<-- "tests/Testcontainers.OpenSearch.Tests/OpenSearchContainerTest.cs:SslBasicAuthCustomCredentials"
3737
```
3838

39-
How to check that client established connection:
40-
=== "PingExample"
39+
How to check if the client has established a connection:
40+
41+
=== "Ping example"
4142
```csharp
4243
--8<-- "tests/Testcontainers.OpenSearch.Tests/OpenSearchContainerTest.cs:PingExample"
4344
```
4445

45-
Creating index and index alias:
46-
=== "IndexAndAliasCreation"
46+
Creating an index and alias:
47+
48+
=== "Create index and alias"
4749
```csharp
48-
--8<-- "tests/Testcontainers.OpenSearch.Tests/OpenSearchContainerTest.cs:IndexAndAliasCreation"
50+
--8<-- "tests/Testcontainers.OpenSearch.Tests/OpenSearchContainerTest.cs:CreateIndexAndAlias"
4951
```
50-
=== "CreateTestIndexImpl"
52+
=== "Create index implementation"
5153
```csharp
52-
--8<-- "tests/Testcontainers.OpenSearch.Tests/OpenSearchContainerTest.cs:CreateTestIndexImpl"
54+
--8<-- "tests/Testcontainers.OpenSearch.Tests/OpenSearchContainerTest.cs:CreateIndexImplementation"
5355
```
5456

55-
Indexing and searching for document:
56-
=== "IndexingDocument"
57+
Indexing and searching a document:
58+
59+
=== "Indexing document"
5760
```csharp
5861
--8<-- "tests/Testcontainers.OpenSearch.Tests/OpenSearchContainerTest.cs:IndexingDocument"
5962
```

src/Testcontainers.OpenSearch/OpenSearchBuilder.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ public sealed class OpenSearchBuilder : ContainerBuilder<OpenSearchBuilder, Open
66
{
77
public const string OpenSearchImage = "opensearchproject/opensearch:2.12.0";
88

9-
public const int OpenSearchRestApiPort = 9200;
9+
public const ushort OpenSearchRestApiPort = 9200;
1010

11-
public const int OpenSearchTransportPort = 9300;
11+
public const ushort OpenSearchTransportPort = 9300;
1212

13-
public const int OpenSearchPerformanceAnalyzerPort = 9600;
13+
public const ushort OpenSearchPerformanceAnalyzerPort = 9600;
1414

1515
public const string DefaultUsername = "admin";
1616

@@ -79,22 +79,26 @@ public override OpenSearchContainer Build()
7979
{
8080
Validate();
8181

82-
var openSearchBuilder = this;
82+
OpenSearchBuilder openSearchBuilder;
8383

84-
const string oldInsecurePassword = "admin";
85-
var imageIsBefore_v2_12 = DockerResourceConfiguration.Image.MatchVersion(p => (p.Major == 2 && p.Minor < 12) || p.Major == 1);
86-
// OpenSearch images before v2.12.0 have hardcoded default password.
87-
// If password have not been set (it equals DefaultPassword), set it to "admin".
88-
if (imageIsBefore_v2_12 && string.Equals(DockerResourceConfiguration.Password, DefaultPassword, StringComparison.Ordinal))
84+
Predicate<System.Version> predicate = v => v.Major == 1 || (v.Major == 2 && v.Minor < 12);
85+
86+
var image = DockerResourceConfiguration.Image;
87+
88+
// Images before version 2.12.0 use a hardcoded default password.
89+
var requiresHardcodedDefaultPassword = image.MatchVersion(predicate);
90+
if (requiresHardcodedDefaultPassword)
91+
{
92+
openSearchBuilder = WithPassword("admin");
93+
}
94+
else
8995
{
90-
openSearchBuilder = WithPassword(oldInsecurePassword);
96+
openSearchBuilder = this;
9197
}
9298

9399
// By default, the base builder waits until the container is running. However, for OpenSearch, a more advanced waiting strategy is necessary that requires access to the password.
94100
// If the user does not provide a custom waiting strategy, append the default OpenSearch waiting strategy.
95-
openSearchBuilder = DockerResourceConfiguration.WaitStrategies.Count() > 1 ?
96-
openSearchBuilder :
97-
openSearchBuilder.WithWaitStrategy(Wait.ForUnixContainer().AddCustomWaitStrategy(new WaitUntil(openSearchBuilder.DockerResourceConfiguration)));
101+
openSearchBuilder = DockerResourceConfiguration.WaitStrategies.Count() > 1 ? openSearchBuilder : openSearchBuilder.WithWaitStrategy(Wait.ForUnixContainer().AddCustomWaitStrategy(new WaitUntil(DockerResourceConfiguration)));
98102
return new OpenSearchContainer(openSearchBuilder.DockerResourceConfiguration);
99103
}
100104

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace Testcontainers.OpenSearch;
2+
3+
public sealed class OpenSearchBuilderTest
4+
{
5+
[Theory]
6+
[InlineData("opensearchproject/opensearch:1.0.0")]
7+
[InlineData("opensearchproject/opensearch:1.1.0")]
8+
[InlineData("opensearchproject/opensearch:2.11.0")]
9+
[InlineData("opensearchproject/opensearch:2.11.1")]
10+
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
11+
public void ShouldUseHardcodedDefaultPassword(string image)
12+
{
13+
// Given
14+
var opensearchContainer = new OpenSearchBuilder().WithImage(image).Build();
15+
16+
// When
17+
var credentials = opensearchContainer.GetCredentials();
18+
19+
// Then
20+
Assert.Equal("admin", credentials.Password);
21+
}
22+
}

tests/Testcontainers.OpenSearch.Tests/OpenSearchContainerLegacyImagesTest.cs

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)