Skip to content

Commit 17d9806

Browse files
committed
chore: Align configuration with TC for Go
1 parent bd60777 commit 17d9806

4 files changed

Lines changed: 20 additions & 143 deletions

File tree

src/Testcontainers.CosmosDb/CosmosDbBuilder.cs

Lines changed: 4 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public sealed class CosmosDbBuilder : ContainerBuilder<CosmosDbBuilder, CosmosDb
88
public const string CosmosDbImage = "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:vnext-preview";
99

1010
public const ushort CosmosDbPort = 8081;
11-
public const ushort CosmosDbHealthCheckPort = 8080;
11+
1212
public const string DefaultAccountKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
1313

1414
/// <summary>
@@ -77,10 +77,10 @@ protected override CosmosDbBuilder Init()
7777
{
7878
return base.Init()
7979
.WithPortBinding(CosmosDbPort, true)
80-
.WithPortBinding(CosmosDbHealthCheckPort, true)
8180
.WithEnvironment("ENABLE_EXPLORER", "false")
81+
.WithEnvironment("ENABLE_TELEMETRY", "false")
8282
.WithConnectionStringProvider(new CosmosDbConnectionStringProvider())
83-
.WithWaitStrategy(Wait.ForUnixContainer().AddCustomWaitStrategy(new WaitUntil()));
83+
.WithWaitStrategy(Wait.ForUnixContainer().UntilMessageIsLogged("Started"));
8484
}
8585

8686
/// <inheritdoc />
@@ -100,68 +100,4 @@ protected override CosmosDbBuilder Merge(CosmosDbConfiguration oldValue, CosmosD
100100
{
101101
return new CosmosDbBuilder(new CosmosDbConfiguration(oldValue, newValue));
102102
}
103-
104-
/// <inheritdoc cref="IWaitUntil" />
105-
private sealed class WaitUntil : IWaitUntil
106-
{
107-
/// <inheritdoc />
108-
public async Task<bool> UntilAsync(IContainer container)
109-
{
110-
// CosmosDB's preconfigured HTTP client will redirect the request to the container.
111-
const string RequestUri = "http://localhost/alive";
112-
using var httpClient = ((CosmosDbContainer)container).HttpClientHealthCheck;
113-
114-
try
115-
{
116-
using var httpResponse = await httpClient
117-
.GetAsync(RequestUri)
118-
.ConfigureAwait(false);
119-
120-
/*
121-
Example response from CosmosDB Emulator's /alive endpoint:
122-
123-
HTTP/1.1 200 OK
124-
Content-Type: application/json
125-
Connection: close
126-
Content-Length: 280
127-
128-
{
129-
"alive": true,
130-
"checks": {
131-
"explorer": "disabled",
132-
"gateway": "healthy",
133-
"postgres": "healthy"
134-
},
135-
"overall": true,
136-
"protocol": "http",
137-
"ready": true,
138-
"status": "healthy",
139-
"timestamp": "2026-02-28T08:52:40.328000942+00:00",
140-
"version": "EN20260227"
141-
}
142-
143-
The following conditions will check that the endpoint returns a success status code
144-
and that the "gateway" and "postgres" checks are healthy, which indicates that the CosmosDB Emulator is ready to accept requests.
145-
This is because sometimes the /alive endpoint may return a successful response before the CosmosDB Emulator is fully ready
146-
to accept requests. Checking the "gateway" and "postgres" checks can provide a more reliable indication of readiness.
147-
*/
148-
if (httpResponse.IsSuccessStatusCode)
149-
{
150-
var content = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
151-
using var jsonDocument = System.Text.Json.JsonDocument.Parse(content);
152-
if (jsonDocument.RootElement.TryGetProperty("checks", out var checksProperty) &&
153-
checksProperty.TryGetProperty("gateway", out var gatewayProperty) &&
154-
"healthy".Equals(gatewayProperty.GetString(), StringComparison.OrdinalIgnoreCase) &&
155-
checksProperty.TryGetProperty("postgres", out var postgresProperty) &&
156-
"healthy".Equals(postgresProperty.GetString(), StringComparison.OrdinalIgnoreCase))
157-
{
158-
return true;
159-
}
160-
}
161-
}
162-
catch { }
163-
164-
return false;
165-
}
166-
}
167-
}
103+
}

src/Testcontainers.CosmosDb/CosmosDbContainer.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,15 @@ public string GetConnectionString()
2626
}
2727

2828
/// <summary>
29-
/// Gets a configured HTTP message handler for the CosmosDb Emulator instance.
29+
/// Gets a configured HTTP message handler that automatically trusts the CosmosDb Emulator's certificate.
3030
/// </summary>
3131
public HttpMessageHandler HttpMessageHandler => new UriRewriter(Hostname, GetMappedPublicPort(CosmosDbBuilder.CosmosDbPort));
3232

3333
/// <summary>
34-
/// Gets a configured HTTP client for the CosmosDb Emulator instance.
34+
/// Gets a configured HTTP client that automatically trusts the CosmosDb Emulator's certificate.
3535
/// </summary>
3636
public HttpClient HttpClient => new HttpClient(HttpMessageHandler);
3737

38-
/// <summary>
39-
/// Gets a configured HTTP message handler for health checks.
40-
/// </summary>
41-
internal HttpMessageHandler HttpMessageHealthCheckHandler => new UriRewriter(Hostname, GetMappedPublicPort(CosmosDbBuilder.CosmosDbHealthCheckPort));
42-
43-
/// <summary>
44-
/// Gets a configured HTTP client for health checks.
45-
/// </summary>
46-
internal HttpClient HttpClientHealthCheck => new HttpClient(HttpMessageHealthCheckHandler);
47-
4838
/// <summary>
4939
/// Rewrites the HTTP requests to target the running CosmosDb Emulator instance.
5040
/// </summary>
@@ -73,4 +63,4 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
7363
return base.SendAsync(request, cancellationToken);
7464
}
7565
}
76-
}
66+
}

src/Testcontainers.CosmosDb/Testcontainers.CosmosDb.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
<ItemGroup>
1010
<ProjectReference Include="../Testcontainers/Testcontainers.csproj"/>
1111
</ItemGroup>
12-
</Project>
12+
</Project>
Lines changed: 12 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.Linq;
3-
4-
namespace Testcontainers.CosmosDb.Tests;
1+
namespace Testcontainers.CosmosDb;
52

63
public sealed class CosmosDbContainerTest : IAsyncLifetime
74
{
@@ -20,69 +17,23 @@ public ValueTask DisposeAsync()
2017

2118
[Fact]
2219
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
23-
public async Task CreateDatabaseAndContainerSuccessful()
20+
public async Task AccountPropertiesIdReturnsLocalhost()
2421
{
2522
// Given
26-
using var cosmosClient = new CosmosClient(
27-
_cosmosDbContainer.GetConnectionString(),
28-
new()
29-
{
30-
ConnectionMode = ConnectionMode.Gateway,
31-
HttpClientFactory = () => _cosmosDbContainer.HttpClient
32-
});
33-
34-
35-
// When
36-
var database = (await cosmosClient.CreateDatabaseIfNotExistsAsync("fakedb")).Database;
37-
await database.CreateContainerIfNotExistsAsync("fakecontainer", "/id");
38-
var databaseProperties = (await database.ReadAsync()).Resource;
23+
using var httpClient = _cosmosDbContainer.HttpClient;
3924

25+
var cosmosClientOptions = new CosmosClientOptions();
26+
cosmosClientOptions.ConnectionMode = CosmosConnectionMode.Gateway;
27+
cosmosClientOptions.HttpClientFactory = () => httpClient;
4028

41-
// Then
42-
Assert.Equal("fakedb", databaseProperties.Id);
43-
}
44-
45-
46-
[Fact]
47-
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
48-
public async Task RetrieveItemCreated()
49-
{
50-
// Given
51-
using var cosmosClient = new CosmosClient(
52-
_cosmosDbContainer.GetConnectionString(),
53-
new()
54-
{
55-
ConnectionMode = ConnectionMode.Gateway,
56-
HttpClientFactory = () => _cosmosDbContainer.HttpClient
57-
});
58-
59-
var database = (await cosmosClient.CreateDatabaseIfNotExistsAsync("dbfake")).Database;
60-
await database.CreateContainerIfNotExistsAsync("containerfake", "/id");
61-
var container = database.GetContainer("containerfake");
62-
63-
var id = Guid.NewGuid().ToString();
64-
var name = Guid.NewGuid().ToString();
65-
29+
using var cosmosClient = new CosmosClient(_cosmosDbContainer.GetConnectionString(), cosmosClientOptions);
6630

6731
// When
68-
await container.CreateItemAsync(
69-
new FakeItem { id = id, Name = name },
70-
new PartitionKey(id));
71-
72-
var itemResponse = await container.ReadItemAsync<FakeItem>(
73-
id,
74-
new PartitionKey(id));
75-
32+
var accountProperties = await cosmosClient.ReadAccountAsync()
33+
.ConfigureAwait(true);
7634

7735
// Then
78-
Assert.Equal(id, itemResponse.Resource.id);
79-
Assert.Equal(name, itemResponse.Resource.Name);
80-
}
81-
82-
83-
private class FakeItem
84-
{
85-
public string id { get; set; }
86-
public string Name { get; set; }
36+
Assert.Equal("cosmosdev", accountProperties.Id);
37+
Assert.Equal(_cosmosDbContainer.GetConnectionString(), _cosmosDbContainer.GetConnectionString(TestcontainersConnectionMode.Host));
8738
}
88-
}
39+
}

0 commit comments

Comments
 (0)