diff --git a/src/Testcontainers.CosmosDb/CosmosDbBuilder.cs b/src/Testcontainers.CosmosDb/CosmosDbBuilder.cs index bde981f22..0dca5d28d 100644 --- a/src/Testcontainers.CosmosDb/CosmosDbBuilder.cs +++ b/src/Testcontainers.CosmosDb/CosmosDbBuilder.cs @@ -4,7 +4,7 @@ namespace Testcontainers.CosmosDb; [PublicAPI] public sealed class CosmosDbBuilder : ContainerBuilder { - public const string CosmosDbImage = "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latest"; + public const string CosmosDbImage = "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:vnext-preview"; public const ushort CosmosDbPort = 8081; @@ -44,6 +44,7 @@ protected override CosmosDbBuilder Init() { return base.Init() .WithImage(CosmosDbImage) + .WithEnvironment("ENABLE_EXPLORER", "false") .WithPortBinding(CosmosDbPort, true) .WithWaitStrategy(Wait.ForUnixContainer().AddCustomWaitStrategy(new WaitUntil())); } @@ -73,25 +74,24 @@ private sealed class WaitUntil : IWaitUntil public async Task UntilAsync(IContainer container) { // CosmosDB's preconfigured HTTP client will redirect the request to the container. - const string requestUri = "https://localhost/_explorer/emulator.pem"; + const string REQUEST_URI = "http://localhost"; - var httpClient = ((CosmosDbContainer)container).HttpClient; + using var httpClient = ((CosmosDbContainer)container).HttpClient; try { - using var httpResponse = await httpClient.GetAsync(requestUri) + using var httpResponse = await httpClient.GetAsync(REQUEST_URI) .ConfigureAwait(false); - return httpResponse.IsSuccessStatusCode; - } - catch (Exception) - { - return false; - } - finally - { - httpClient.Dispose(); + if (httpResponse.IsSuccessStatusCode) + { + await Task.Delay(2_000); + return true; + } } + catch { } + + return false; } } -} \ No newline at end of file +} diff --git a/src/Testcontainers.CosmosDb/CosmosDbContainer.cs b/src/Testcontainers.CosmosDb/CosmosDbContainer.cs index b7b0a57d7..36946d4af 100644 --- a/src/Testcontainers.CosmosDb/CosmosDbContainer.cs +++ b/src/Testcontainers.CosmosDb/CosmosDbContainer.cs @@ -20,7 +20,7 @@ public CosmosDbContainer(CosmosDbConfiguration configuration) public string GetConnectionString() { var properties = new Dictionary(); - properties.Add("AccountEndpoint", new UriBuilder(Uri.UriSchemeHttps, Hostname, GetMappedPublicPort(CosmosDbBuilder.CosmosDbPort)).ToString()); + properties.Add("AccountEndpoint", new UriBuilder(Uri.UriSchemeHttp, Hostname, GetMappedPublicPort(CosmosDbBuilder.CosmosDbPort)).ToString()); properties.Add("AccountKey", CosmosDbBuilder.DefaultAccountKey); return string.Join(";", properties.Select(property => string.Join("=", property.Key, property.Value))); } @@ -50,7 +50,7 @@ private sealed class UriRewriter : DelegatingHandler /// The target hostname. /// The target port. public UriRewriter(string hostname, ushort port) - : base(new HttpClientHandler { ServerCertificateCustomValidationCallback = (_, _, _, _) => true }) + : base(new HttpClientHandler()) { _hostname = hostname; _port = port; @@ -59,8 +59,8 @@ public UriRewriter(string hostname, ushort port) /// protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { - request.RequestUri = new UriBuilder(Uri.UriSchemeHttps, _hostname, _port, request.RequestUri.PathAndQuery).Uri; + request.RequestUri = new UriBuilder(Uri.UriSchemeHttp, _hostname, _port, request.RequestUri.PathAndQuery).Uri; return base.SendAsync(request, cancellationToken); } } -} \ No newline at end of file +} diff --git a/src/Testcontainers.CosmosDb/Testcontainers.CosmosDb.csproj b/src/Testcontainers.CosmosDb/Testcontainers.CosmosDb.csproj index 9a25b9c4d..7abec513c 100644 --- a/src/Testcontainers.CosmosDb/Testcontainers.CosmosDb.csproj +++ b/src/Testcontainers.CosmosDb/Testcontainers.CosmosDb.csproj @@ -9,4 +9,4 @@ - \ No newline at end of file + diff --git a/tests/Testcontainers.CosmosDb.Tests/CosmosDbContainerTest.cs b/tests/Testcontainers.CosmosDb.Tests/CosmosDbContainerTest.cs index 16ee4f4ee..dd16ff265 100644 --- a/tests/Testcontainers.CosmosDb.Tests/CosmosDbContainerTest.cs +++ b/tests/Testcontainers.CosmosDb.Tests/CosmosDbContainerTest.cs @@ -1,4 +1,7 @@ -namespace Testcontainers.CosmosDb; +using System; +using System.Linq; + +namespace Testcontainers.CosmosDb.Tests; public sealed class CosmosDbContainerTest : IAsyncLifetime { @@ -14,24 +17,71 @@ public Task DisposeAsync() return _cosmosDbContainer.DisposeAsync().AsTask(); } - [Fact(Skip = "The Cosmos DB Linux Emulator Docker image does not run on Microsoft's CI environment (GitHub, Azure DevOps).")] // https://github.com/Azure/azure-cosmos-db-emulator-docker/issues/45. + [Fact] + [Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))] + public async Task CreateDatabaseAndContainerSuccessful() + { + // Given + using var cosmosClient = new CosmosClient( + _cosmosDbContainer.GetConnectionString(), + new() + { + ConnectionMode = ConnectionMode.Gateway, + HttpClientFactory = () => _cosmosDbContainer.HttpClient + }); + + + // When + var database = (await cosmosClient.CreateDatabaseIfNotExistsAsync("fakedb")).Database; + await database.CreateContainerIfNotExistsAsync("fakecontainer", "/id"); + var databaseProperties = (await cosmosClient.GetDatabaseQueryIterator().ReadNextAsync()).First(); + + + // Then + Assert.Equal("fakedb", databaseProperties.Id); + } + + + [Fact] [Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))] - public async Task AccountPropertiesIdReturnsLocalhost() + public async Task RetrieveItemCreated() { // Given - using var httpClient = _cosmosDbContainer.HttpClient; + using var cosmosClient = new CosmosClient( + _cosmosDbContainer.GetConnectionString(), + new() + { + ConnectionMode = ConnectionMode.Gateway, + HttpClientFactory = () => _cosmosDbContainer.HttpClient + }); - var cosmosClientOptions = new CosmosClientOptions(); - cosmosClientOptions.ConnectionMode = ConnectionMode.Gateway; - cosmosClientOptions.HttpClientFactory = () => httpClient; + var database = (await cosmosClient.CreateDatabaseIfNotExistsAsync("dbfake")).Database; + await database.CreateContainerIfNotExistsAsync("containerfake", "/id"); + var container = database.GetContainer("containerfake"); + + var id = Guid.NewGuid().ToString(); + var name = Guid.NewGuid().ToString(); - using var cosmosClient = new CosmosClient(_cosmosDbContainer.GetConnectionString(), cosmosClientOptions); // When - var accountProperties = await cosmosClient.ReadAccountAsync() - .ConfigureAwait(true); + var response = await container.CreateItemAsync( + new FakeItem { id = id, Name = name }, + new PartitionKey(id)); + + var itemResponse = await container.ReadItemAsync( + id, + new PartitionKey(id)); + // Then - Assert.Equal("localhost", accountProperties.Id); + Assert.Equal(id, itemResponse.Resource.id); + Assert.Equal(name, itemResponse.Resource.Name); + } + + + private class FakeItem + { + public string id { get; set; } + public string Name { get; set; } } -} \ No newline at end of file +}