Skip to content
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

Testcontainer for CosmosDB Emulator #549

Closed
Closed
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
fcf80d6
Add CosmosDBTestcontainerConfiguration
Yeseh Jul 25, 2022
75bfbf3
Implement CosmosDbTestContainer
Yeseh Jul 25, 2022
301c551
Implement CosmosDbFixture
Yeseh Jul 25, 2022
32803e2
Write first test (not working)
Yeseh Jul 25, 2022
6d0ae41
Working initial setup, database creation
Yeseh Aug 2, 2022
c3809a4
Merge branch 'develop' of https://github.com/testcontainers/testconta…
Yeseh Aug 28, 2022
dad2dc9
Restructure to model Azurite testcontainer
Yeseh Aug 28, 2022
0f429ba
fix tests + cleanup
ktjn Sep 6, 2022
3fc1c18
Merge branch 'develop' into feature/cosmosdb-testcontainer
ktjn Sep 6, 2022
b1dff3f
(temp hack) wait for max 5 min
ktjn Sep 8, 2022
d38123a
Merge branch 'feature/cosmosdb-testcontainer' of https://github.com/Y…
ktjn Sep 8, 2022
4c45cd0
add debug logging
ktjn Sep 8, 2022
5a66c99
cleanup debug code
ktjn Sep 8, 2022
a21b5e4
set timeout on build
ktjn Sep 8, 2022
56dbe8f
first cleanup sweep
ktjn Sep 8, 2022
8cdb84e
more cleanup and tests
ktjn Sep 9, 2022
4ecdf9d
bail directly if cosmos is shut down
ktjn Sep 9, 2022
4f07e0e
try ubuntu 22
ktjn Sep 9, 2022
140a865
revert ubuntu 22, add restart hack
ktjn Sep 9, 2022
da7625d
clean up
ktjn Sep 9, 2022
246b4d9
try setting ip for cosmos
ktjn Sep 9, 2022
737708b
minor cleanups
ktjn Sep 9, 2022
88cf029
cleanup
ktjn Sep 10, 2022
bb7d473
Merge branch 'develop' into feature/cosmosdb-testcontainer
ktjn Sep 12, 2022
f349512
skip cosmos tests when running docker on a linux host
ktjn Sep 12, 2022
1a2c072
dummy change to trigger build
ktjn Sep 12, 2022
bf70b6c
Revert "skip cosmos tests when running docker on a linux host"
ktjn Sep 13, 2022
2f29611
cleanup
ktjn Sep 14, 2022
bf79586
add wait timeout
ktjn Sep 15, 2022
9043765
Merge branch 'develop' into feature/cosmosdb-testcontainer
ktjn Sep 15, 2022
0d77571
debug values removed
ktjn Sep 15, 2022
f5a4559
Merge branch 'feature/cosmosdb-testcontainer' of https://github.com/Y…
ktjn Sep 15, 2022
498e37b
cleanup
ktjn Sep 15, 2022
90dec5f
Revert "cleanup"
ktjn Sep 16, 2022
3b90301
Revert "debug values removed"
ktjn Sep 16, 2022
f0837d4
Revert "add wait timeout"
ktjn Sep 16, 2022
d3c1067
skip tests
ktjn Sep 16, 2022
9dcac2a
more cleanup
ktjn Sep 16, 2022
c6d2f81
Merge branch 'develop' into feature/cosmosdb-testcontainer
ktjn Sep 24, 2022
6e7fe69
fix environment init
ktjn Sep 25, 2022
0730fda
Merge branch 'feature/cosmosdb-testcontainer' of https://github.com/Y…
ktjn Sep 25, 2022
50fbb80
Merge branch 'develop' into feature/cosmosdb-testcontainer
ktjn Sep 26, 2022
ad4c507
expose HttpMessageHandler
ktjn Sep 26, 2022
db53567
Merge branch 'feature/cosmosdb-testcontainer' of https://github.com/Y…
ktjn Sep 26, 2022
93e8666
Merge branch 'develop' into feature/cosmosdb-testcontainer
ktjn Oct 1, 2022
297df85
review fixes
ktjn Oct 2, 2022
b5444ee
create waitstrategy in constructor
ktjn Oct 3, 2022
44687bf
ref cosmosdb emulator bug
ktjn Oct 3, 2022
a9d0151
cleanup
ktjn Oct 3, 2022
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 Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<PackageReference Update="Azure.Data.Tables" Version="12.6.1" />
<PackageReference Update="Azure.Storage.Blobs" Version="12.13.0" />
<PackageReference Update="Azure.Storage.Queues" Version="12.11.0" />
<PackageReference Update="Microsoft.Azure.Cosmos" Version="3.30.0" />
<PackageReference Update="Confluent.Kafka" Version="1.8.2" />
<PackageReference Update="CouchbaseNetClient" Version="3.2.9" />
<PackageReference Update="Elastic.Clients.Elasticsearch" Version="8.0.0-beta.3" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
namespace DotNet.Testcontainers.Configurations
{
using System;
using System.Globalization;
using System.IO;
using DotNet.Testcontainers.Builders;
using JetBrains.Annotations;

[PublicAPI]
public sealed class CosmosDbTestcontainerConfiguration : TestcontainerDatabaseConfiguration
{
public const string DefaultCosmosDbApiImage =
"mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator";
HofmeisterAn marked this conversation as resolved.
Show resolved Hide resolved

private const int DefaultCosmosPort = 8081;

public CosmosDbTestcontainerConfiguration()
: this(DefaultCosmosDbApiImage)
{
}

public CosmosDbTestcontainerConfiguration(string image)
: base(image, DefaultCosmosPort)
{
this.PartitionCount = 2;
this.IpAddressOverride = "127.0.0.1";
this.OutputConsumer = Consume.RedirectStdoutAndStderrToStream(new MemoryStream(), new MemoryStream());
this.Database = "default";
}

public override IOutputConsumer OutputConsumer { get; }

public override IWaitForContainerOS WaitStrategy => Wait.ForUnixContainer().UntilMessageIsLogged(this.OutputConsumer.Stdout, "Started|Shutting");

public override string Password
{
// Default key for the emulator
// See: https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator?tabs=ssl-netstd21#authenticate-requests
get => "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
set => throw new NotImplementedException();
}

public override string Database
{
get => this.Environments["AZURE_COSMOS_EMULATOR_DATABASE"];
set => this.Environments["AZURE_COSMOS_EMULATOR_DATABASE"] = value;
}

public int PartitionCount
{
get => int.TryParse(this.Environments["AZURE_COSMOS_EMULATOR_PARTITION_COUNT"], NumberStyles.Integer, CultureInfo.InvariantCulture, out var partitionCount) ? partitionCount : 1;
set => this.Environments["AZURE_COSMOS_EMULATOR_PARTITION_COUNT"] = value.ToString(CultureInfo.InvariantCulture);
}

public bool EnableDataPersistence
{
get => bool.TryParse(this.Environments["AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE"], out var enableDataPersistence) && enableDataPersistence;
set => this.Environments["AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE"] = value.ToString().ToLowerInvariant();
}

public string IpAddressOverride
{
get => this.Environments["AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE"];
set => this.Environments["AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE"] = value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace DotNet.Testcontainers.Containers
{
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using DotNet.Testcontainers.Configurations;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;

[PublicAPI]
public sealed class CosmosDbTestcontainer : TestcontainerDatabase
HofmeisterAn marked this conversation as resolved.
Show resolved Hide resolved
{
internal CosmosDbTestcontainer(ITestcontainersConfiguration configuration, ILogger logger)
: base(configuration, logger)
{
}

public HttpMessageHandler HttpMessageHandler => new UriRewriter(this.Hostname, this.Port);
HofmeisterAn marked this conversation as resolved.
Show resolved Hide resolved

public HttpClient HttpClient
{
get
{
return new HttpClient(new UriRewriter(this.Hostname, this.Port));
}
}

public override string ConnectionString =>
$"AccountEndpoint=https://{this.Hostname}:{this.Port};AccountKey={this.Password}";

private sealed class UriRewriter : DelegatingHandler
{
private readonly string hostname;

private readonly int port;

public UriRewriter(string hostname, int port)
: base(new HttpClientHandler { ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true })
{
this.hostname = hostname;
this.port = port;
}

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.RequestUri = new UriBuilder("https", this.hostname, this.port, request.RequestUri.PathAndQuery).Uri;
return base.SendAsync(request, cancellationToken);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace DotNet.Testcontainers.Tests.Fixtures
{
using System;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Containers;
using JetBrains.Annotations;
using Microsoft.Azure.Cosmos;

[UsedImplicitly]
public sealed class CosmosDbFixture : DatabaseFixture<CosmosDbTestcontainer, CosmosClient>
{
private readonly CosmosDbTestcontainerConfiguration configuration = new CosmosDbTestcontainerConfiguration();

private readonly CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromMinutes(5));

public CosmosDbFixture()
{
this.Container = new TestcontainersBuilder<CosmosDbTestcontainer>()
.WithDatabase(this.configuration)
.Build();
}

public override Task InitializeAsync()
{
return this.Container.StartAsync(this.cts.Token);
}

public override Task DisposeAsync()
{
return this.Container.DisposeAsync().AsTask();
}

public override void Dispose()
{
this.configuration.Dispose();
}
}
}
1 change: 1 addition & 0 deletions tests/Testcontainers.Tests/Testcontainers.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<PackageReference Include="Azure.Data.Tables" />
<PackageReference Include="Azure.Storage.Blobs" />
<PackageReference Include="Azure.Storage.Queues" />
<PackageReference Include="Microsoft.Azure.Cosmos" />
<PackageReference Include="Confluent.Kafka" />
<PackageReference Include="CouchbaseNetClient" />
<PackageReference Include="Elastic.Clients.Elasticsearch" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
namespace DotNet.Testcontainers.Tests.Unit
{
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using DotNet.Testcontainers.Tests.Fixtures;
using Microsoft.Azure.Cosmos;
using Xunit;

public static class CosmosDbTestcontainerTest
{
[Collection(nameof(Testcontainers))]
public sealed class ConnectionTests : IClassFixture<CosmosDbFixture>, IDisposable
{
private readonly HttpClient httpClient;

private readonly CosmosClient cosmosClient;

public ConnectionTests(CosmosDbFixture cosmosDbFixture)
: this(cosmosDbFixture.Container.HttpClient, cosmosDbFixture.Container.ConnectionString)
{
}

private ConnectionTests(HttpClient httpClient, string connectionString)
{
var cosmosClientOptions = new CosmosClientOptions { ConnectionMode = ConnectionMode.Gateway, HttpClientFactory = () => httpClient };
this.httpClient = httpClient;
this.cosmosClient = new CosmosClient(connectionString, cosmosClientOptions);
}

[Fact(Skip = "Waiting for a working cosmosdb emulator")]
HofmeisterAn marked this conversation as resolved.
Show resolved Hide resolved
public async Task ShouldEstablishConnection()
{
var accountProperties = await this.cosmosClient.ReadAccountAsync()
.ConfigureAwait(false);

Assert.Equal("localhost", accountProperties.Id);
}

[Fact(Skip = "Waiting for a working cosmosdb emulator")]
public async Task CreateDatabaseTest()
{
var databaseResponse = await this.cosmosClient.CreateDatabaseIfNotExistsAsync("db")
.ConfigureAwait(false);

Assert.Equal(HttpStatusCode.Created, databaseResponse.StatusCode);
}

public void Dispose()
{
this.cosmosClient.Dispose();
this.httpClient.Dispose();
}
}
}
}