testcontainers-dotnet icon indicating copy to clipboard operation
testcontainers-dotnet copied to clipboard

Add support for CosmosDB Emulator and Storage Emulator (Azurite)

Open d-m4m4n opened this issue 3 years ago • 23 comments

Is your feature request related to a problem? Please describe. Not related to a problem

Describe the solution you'd like I would like to see setups for CosmosDB Emulator and Azurite to allow usage for testing storage or cosmosDB functions.

Describe alternatives you've considered I have one implemented locally before I found this repo. I can contribute and add these changes but need to make sure I find everything I need to make sure it works. I see there is ExecuteScripts functions and I don't think they come with CLIs in the images so I would have to check.

Additional context

d-m4m4n avatar Feb 14 '22 20:02 d-m4m4n

Thanks for your feature request. Supporting Azure Cosmos DB would be great.

Right now, .NET Testcontainers does not ship any 3rd party database or message broker libraries etc. This makes it easier to maintain the library, but it has a disadvantage. All vendor specific calls, features, etc. go straight to the container. We do not use any client libraries at all.

I'd like to move modules in the future (databases, message brokers, etc.) to their own library. This allows much better support incl. features for databases, message brokers, etc.

What do you think about a specific project, like DotNet.Testcontainers.CosmosDB?

HofmeisterAn avatar Feb 15 '22 08:02 HofmeisterAn

Thanks for your feature request. Supporting Azure Cosmos DB would be great.

Right now, .NET Testcontainers does not ship any 3rd party database or message broker libraries etc. This makes it easier to maintain the library, but it has a disadvantage. All vendor specific calls, features, etc. go straight to the container. We do not use any client libraries at all.

I'd like to move modules in the future (databases, message brokers, etc.) to their own library. This allows much better support incl. features for databases, message brokers, etc.

What do you think about a specific project, like DotNet.Testcontainers.CosmosDB?

That would be great! Really anything to take advantage of all the awesome work you have done here. Maybe DotNet.Testcontainers.CosmosDB and DotNet.Testcontainers.Azurite ?

d-m4m4n avatar Feb 15 '22 17:02 d-m4m4n

Yep, sounds good.

HofmeisterAn avatar Feb 17 '22 19:02 HofmeisterAn

Built in integration would be a god send!

I'm having trouble trying to spin up an instance of CosmosDb - I'm not able to access the cosmos explorer, do you know if the cosmos image should be compatible with this library?

Running this command works fine: docker run --name=Cosmos_DB -d -p 8081:8081 -p 10251:10251 -p 10252:10252 -p 10253:10253 -p 10254:10254 -e AZURE_COSMOS_EMULATOR_PARTITION_COUNT=29 -e AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE=127.0.0.1 -e AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator

However the following creates the container but I can't connect to the explorer:

    var cosmosDbContainerBuilder = new TestcontainersBuilder<TestcontainersContainer>()
        .WithImage("mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator")
        .WithName("Cosmos_DB")
        .WithPortBinding(8081, 8081)
        .WithPortBinding(10251, 10251)
        .WithPortBinding(10252, 10252)
        .WithPortBinding(10253, 10253)
        .WithPortBinding(10254, 10254)
        .WithEnvironment("AZURE_COSMOS_EMULATOR_PARTITION_COUNT", "3")
        .WithEnvironment("AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE", "127.0.0.1")
        .WithEnvironment("AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE", "false")
        .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(8081));

    await using (var cosmosDbContainer = cosmosDbContainerBuilder.Build())
    {
        await cosmosDbContainer.StartAsync();

        //...
    }

Timmoth avatar Feb 24 '22 16:02 Timmoth

You need to expose the port 8081 too. It's not exposed by the Dockerfile. Then I recommend assigning random ports.

In addition to that, it's not enough to just wait for port 8081. You'll need a custom strategy to figure out when the container is up and running:

private readonly ITestcontainersContainer container = new TestcontainersBuilder<TestcontainersContainer>()
  .WithImage("mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator")
  .WithName("azure-cosmos-emulator")
  .WithExposedPort(8081)
  .WithPortBinding(8081, true)
  .WithPortBinding(10251, true)
  .WithPortBinding(10252, true)
  .WithPortBinding(10253, true)
  .WithPortBinding(10254, true)
  .WithEnvironment("AZURE_COSMOS_EMULATOR_PARTITION_COUNT", "1")
  .WithEnvironment("AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE", "127.0.0.1")
  .WithEnvironment("AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE", "false")
  .WithWaitStrategy(Wait.ForUnixContainer()
    .UntilPortIsAvailable(8081))
  .Build();

[Fact]
public async Task Issue()
{
  // TODO: You need to replace this with a proper wait strategy. Port 8081 is accessible before the container is ready.
  await Task.Delay(TimeSpan.FromSeconds(30))
    .ConfigureAwait(false);

  using (var handler = new HttpClientHandler())
  {
    handler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true;

    using (var client = new HttpClient(handler))
    {
      var mappedPort = this.container.GetMappedPublicPort(8081);

      var response = await client.GetAsync($"https://localhost:{mappedPort}/_explorer/emulator.pem")
        .ConfigureAwait(false);

      var pem = await response.Content.ReadAsStringAsync()
        .ConfigureAwait(false);

      Debug.WriteLine(pem);
    }
  }
}

public Task InitializeAsync()
{
  return this.container.StartAsync();
}

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

HofmeisterAn avatar Feb 24 '22 18:02 HofmeisterAn

You need to expose the port 8081 too. It's not exposed by the Dockerfile. Then I recommend assigning random ports.

In addition to that, it's not enough to just wait for port 8081. You'll need a custom strategy to figure out when the container is up and running:

private readonly ITestcontainersContainer container = new TestcontainersBuilder<TestcontainersContainer>()
  .WithImage("mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator")
  .WithName("azure-cosmos-emulator")
  .WithExposedPort(8081)
  .WithPortBinding(8081, true)
  .WithPortBinding(10251, true)
  .WithPortBinding(10252, true)
  .WithPortBinding(10253, true)
  .WithPortBinding(10254, true)
  .WithEnvironment("AZURE_COSMOS_EMULATOR_PARTITION_COUNT", "1")
  .WithEnvironment("AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE", "127.0.0.1")
  .WithEnvironment("AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE", "false")
  .WithWaitStrategy(Wait.ForUnixContainer()
    .UntilPortIsAvailable(8081))
  .Build();

[Fact]
public async Task Issue()
{
  // TODO: You need to replace this with a proper wait strategy. Port 8081 is accessible before the container is ready.
  await Task.Delay(TimeSpan.FromSeconds(30))
    .ConfigureAwait(false);

  using (var handler = new HttpClientHandler())
  {
    handler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true;

    using (var client = new HttpClient(handler))
    {
      var mappedPort = this.container.GetMappedPublicPort(8081);

      var response = await client.GetAsync($"https://localhost:{mappedPort}/_explorer/emulator.pem")
        .ConfigureAwait(false);

      var pem = await response.Content.ReadAsStringAsync()
        .ConfigureAwait(false);

      Debug.WriteLine(pem);
    }
  }
}

public Task InitializeAsync()
{
  return this.container.StartAsync();
}

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

Incredible work, will be looking at implementing this shortly!

d-m4m4n avatar Feb 24 '22 20:02 d-m4m4n

Thanks so much! In case anyone else stumbles across this I managed to get a global instance of cosmos running for my integration tests with the following snippet:

[CollectionDefinition(nameof(IntegrationTestCollection))]
public class IntegrationTestCollection : ICollectionFixture<IntegrationTestCollectionFixture> { }

public sealed class IntegrationTestCollectionFixture : IDisposable
{
    private readonly TestcontainersContainer _cosmosContainer;
    public IntegrationTestCollectionFixture()
    {
        var outputConsumer = Consume.RedirectStdoutAndStderrToStream(new MemoryStream(), new MemoryStream());
        var waitStrategy = Wait.ForUnixContainer().UntilMessageIsLogged(outputConsumer.Stdout, "Started");
        _cosmosContainer = new TestcontainersBuilder<TestcontainersContainer>()
            .WithImage("mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator")
            .WithName("azure-cosmos-emulator")
            .WithExposedPort(8081)
            .WithExposedPort(10251)
            .WithExposedPort(10252)
            .WithExposedPort(10253)
            .WithExposedPort(10254)
            .WithPortBinding(8081, true)
            .WithPortBinding(10251, true)
            .WithPortBinding(10252, true)
            .WithPortBinding(10253, true)
            .WithPortBinding(10254, true)
            .WithEnvironment("AZURE_COSMOS_EMULATOR_PARTITION_COUNT", "30")
            .WithEnvironment("AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE", "127.0.0.1")
            .WithEnvironment("AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE", "false")
            .WithOutputConsumer(outputConsumer)
            .WithWaitStrategy(waitStrategy)
            .Build();

        _cosmosContainer.StartAsync().GetAwaiter().GetResult();
    }

    public void Dispose()
    {
        _ = _cosmosContainer.DisposeAsync();
    }
}

Also the following is needed to setup the CosmosClient

services.AddSingleton(sp =>
{
    var cosmosClientBuilder = new CosmosClientBuilder(connString); 
    cosmosClientBuilder.WithHttpClientFactory(() =>
    {
        HttpMessageHandler httpMessageHandler = new HttpClientHandler()
        {
            ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
        };

        return new HttpClient(httpMessageHandler);
    });
    cosmosClientBuilder.WithConnectionModeGateway();

    return cosmosClientBuilder.Build();
});

See here for more

Timmoth avatar Mar 01 '22 15:03 Timmoth

You need to be aware of the async operations. Your IntegrationTestCollectionFixture is not right. Use the IAsyncLifetime interface of xUnit.net.

public sealed class IntegrationTestCollectionFixture : IAsyncLifetime, IDisposable
{
  private readonly IOutputConsumer consumer = Consume.RedirectStdoutAndStderrToStream(new MemoryStream(), new MemoryStream());

  private readonly ITestcontainersContainer container;

  public IntegrationTestCollectionFixture()
  {
    this.container = new TestcontainersBuilder<TestcontainersContainer>()
      .WithImage("mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator")
      .WithName("azure-cosmos-emulator")
      .WithExposedPort(8081)
      .WithExposedPort(10251)
      .WithExposedPort(10252)
      .WithExposedPort(10253)
      .WithExposedPort(10254)
      .WithPortBinding(8081, true)
      .WithPortBinding(10251, true)
      .WithPortBinding(10252, true)
      .WithPortBinding(10253, true)
      .WithPortBinding(10254, true)
      .WithEnvironment("AZURE_COSMOS_EMULATOR_PARTITION_COUNT", "30")
      .WithEnvironment("AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE", "127.0.0.1")
      .WithEnvironment("AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE", "false")
      .WithOutputConsumer(this.consumer)
      .WithWaitStrategy(Wait.ForUnixContainer()
        .UntilMessageIsLogged(this.consumer.Stdout, "Started"))
      .Build();
  }

  public Task InitializeAsync()
  {
    return this.container.StartAsync();
  }

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

  public void Dispose()
  {
    this.consumer.Dispose();
  }
}

HofmeisterAn avatar Mar 01 '22 16:03 HofmeisterAn

What do you think about a specific project, like DotNet.Testcontainers.CosmosDB?

@HofmeisterAn Do you mean new project in same repo or in dedicated repo? As mentioned in https://github.com/testcontainers/testcontainers-dotnet/issues/421#issuecomment-1039979866

vlaskal avatar Jun 18 '22 07:06 vlaskal

I think it makes sense to keep modules in this repository, but don't include them in the Testcontainers dependency anymore. Each module should be an independent package.

HofmeisterAn avatar Jun 18 '22 21:06 HofmeisterAn

Maybe we can avoid using the Cosmos client SDK (at least for now) by using the REST API (see: https://docs.microsoft.com/en-us/rest/api/cosmos-db/querying-cosmosdb-resources-using-the-rest-api). Would this cover the usecase?

Would love to try my hand at this for a bit if noone else is.

Yeseh avatar Jul 25 '22 21:07 Yeseh

OC, we can use the version without client as MVP and enhance it as soon as the new structure is available.

HofmeisterAn avatar Jul 26 '22 16:07 HofmeisterAn

This thread has been incredibly helpful. Thank you!

I've got my test container starting, and I can see the cosmos db emulator container start up in the docker desktop dashboard however I am struggling to get the test to connect to it.

I think it is down to the connection string.

In the example above ...

services.AddSingleton(sp =>
{
    var cosmosClientBuilder = new CosmosClientBuilder(connString); 
    cosmosClientBuilder.WithHttpClientFactory(() =>
    {
        HttpMessageHandler httpMessageHandler = new HttpClientHandler()
        {
            ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
        };

        return new HttpClient(httpMessageHandler);
    });
    cosmosClientBuilder.WithConnectionModeGateway();

    return cosmosClientBuilder.Build();
});

The string for connString is mentioned but not constructed. Is this the default dev emulator detail or do I need to update it to point to a different port or something? If it's not this, then I am lost 😕

Any help would be appreciated.

Thanks

EDIT

I've updated the code above so that the connection string is created based on the default development one but the port is updated before being provided to the CosmosClientBuilder.

var mappedPort = _dbContainer.GetMappedPublicPort(8081);

var updated = string.Format(connString, mappedPort);

var cosmosClientBuilder = new CosmosClientBuilder(updated);

If I debug the test and pause the test after containers have spun up etc. and I have found the port number from the above I can browse to the data explorer!

Next issue, creating db/containers on the fly for the tests ...

WestDiscGolf avatar Jul 27 '22 11:07 WestDiscGolf

one but the port is updated before being provided to the CosmosClientBuilder.

I assume you refer to the random assigned host port? This is a best practice and avoid clashes. If you assign a random host port WithPortBinding(8081, true) (notice the true arg), you need to get the public mapped port first. Like you have done with GetMappedPublicPort(8081). See this example too: https://github.com/testcontainers/testcontainers-dotnet/discussions/438.

Next issue, creating db/containers on the fly for the tests ...

Usually, that's done via a client dependency or a cli command (ExecAsync).

HofmeisterAn avatar Jul 27 '22 14:07 HofmeisterAn

@HofmeisterAn I opened a Draft PR #549 with some progress, managed to get it up and running and create a database in the container at least. I havn't done any OS before so some early-ish feedback is much appreciated!

Yeseh avatar Aug 02 '22 20:08 Yeseh

@Yeseh @HofmeisterAn I got CosmosDB running in a testcontainer but have some issues with performance. I reached out on twitter and was put in contact with a PM at Microsoft on the cosmos db emulator team. I put together a demo repo to show the setup. Just waiting for a reply.

Please take a look at the repo - https://github.com/WestDiscGolf/CosmosDB-Testcontainers-Test

Hope this helps. I will review the Draft PR as I am interested in seeing how this can be done 😄

WestDiscGolf avatar Aug 02 '22 20:08 WestDiscGolf

Found this which has some good examples of constructing the web requests to call CosmosDB directly which could aid with not using the cosmos client - https://github.com/Azure-Samples/cosmos-db-rest-samples/blob/main/Program.cs

WestDiscGolf avatar Aug 03 '22 09:08 WestDiscGolf

In the examples on how to run the cosmosdb linux emulator in docker it specifies setting the memory and cpu levels ...

docker run \
    --publish 8081:8081 \
    --publish 10251-10254:10251-10254 \
    --memory 3g --cpus=2.0 \
    --name=test-linux-emulator \
    --env AZURE_COSMOS_EMULATOR_PARTITION_COUNT=10 \
    --env AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true \
    --env AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE=$ipaddr \
    --interactive \
    --tty \
    mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator

Docker compose examples seem to suggest the same.

version: '3.4'

services:
  db:
    container_name: cosmosdb
    image: "mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator"
    tty: true
    restart: always
    mem_limit: 2G
    cpu_count: 2
    environment:
      - AZURE_COSMOS_EMULATOR_PARTITION_COUNT=10
      - AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE=true
    ports:
       - "8081:8081"
       - "8900:8900"
       - "8901:8901"
       - "8979:8979"
       - "10250:10250"
       - "10251:10251"
       - "10252:10252"
       - "10253:10253"
       - "10254:10254"
       - "10255:10255"
       - "10256:10256"
       - "10350:10350"
    volumes:
       - vol_cosmos:/data/db

volumes: 
  vol_cosmos:

How can these be specified in the testcontainer setup? Thanks

WestDiscGolf avatar Aug 03 '22 10:08 WestDiscGolf

I havn't done any OS before so some early-ish feedback is much appreciated!

@Yeseh OC, thanks for your contribution. I'll take a look at it in the next days.

Please take a look at the repo - https://github.com/WestDiscGolf/CosmosDB-Testcontainers-Test

@WestDiscGolf please notice, I'm not an CosmosDB expert 😃, but OC, I'll take a look at it.

How can these be specified in the testcontainer setup? Thanks

You can use WithCreateContainerParametersModifier, see #503 and #508.

HofmeisterAn avatar Aug 03 '22 15:08 HofmeisterAn

Cool, thanks @HofmeisterAn. Will have a go with the WithCreateContainerParametersModifier 😄

WestDiscGolf avatar Aug 03 '22 15:08 WestDiscGolf

Found this which has some good examples of constructing the web requests to call CosmosDB directly which could aid with not using the cosmos client - https://github.com/Azure-Samples/cosmos-db-rest-samples/blob/main/Program.cs

Good stuff, that is very useful. Thanks! I'll go implemnent a couple of those at least .

What should the user interface of the Testcontainer be? The above example seems pretty complete, would it be desirable to have all these operations available to the user in the final version (with or without client)?

Yeseh avatar Aug 03 '22 16:08 Yeseh

I see CosmosDB is in progress but Azurite isn't. Are there any objections if I will try work on AzuriteTestcontainer?

vlaskal avatar Aug 04 '22 19:08 vlaskal

@HofmeisterAn Thanks for your feedback on setting up Testcontainers with CosmosDB emulator.

I'm using EF Core Cosmos provider in my application so in xUnit during the test initialization phase I am calling EnsureDeletedAsync() and EnsureCreatedAsync() methods but I am getting these errors:

Gateway HttpRequestException Endpoint not reachable. Failed Location: https://127.0.0.1:8081/; ResourceAddress: dbs/Database

Endpoint https://127.0.0.1:8081/ unavailable for Write added/updated to unavailableEndpoints with timestamp 09/19/2022 15:39:36

I wonder if I would need to specify other local IP address in override of environment variable? Really weird as I am running in Gateway mode

kasparas12 avatar Sep 19 '22 15:09 kasparas12

I wonder if I would need to specify other local IP address in override of environment variable? Really weird as I am running in Gateway mode

@kasparas12 Is 127.0.0.1 a const value? Try the Hostname property and get the random assigned host port via GetMappedPublicPort.

HofmeisterAn avatar Sep 27 '22 10:09 HofmeisterAn

See the CosmosDb emulator PR for more details.

This should work as well:

` public sealed class CosmosDbFixture : IDisposable, IAsyncLifetime { private readonly IOutputConsumer consumer = Consume.RedirectStdoutAndStderrToStream(new MemoryStream(), new MemoryStream()); private readonly ITestcontainersContainer _container;

  public CosmosDbFixture()
  {
      _container = new TestcontainersBuilder<TestcontainersContainer>()
          .WithImage("mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator")
          .WithExposedPort(8081)
          .WithExposedPort(10251)
          .WithExposedPort(10252)
          .WithExposedPort(10253)
          .WithExposedPort(10254)
          .WithPortBinding(8081, true)
          .WithPortBinding(10251, true)
          .WithPortBinding(10252, true)
          .WithPortBinding(10253, true)
          .WithPortBinding(10254, true)
          .WithEnvironment("AZURE_COSMOS_EMULATOR_PARTITION_COUNT", "1")
          .WithEnvironment("AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE", "false")
          .WithOutputConsumer(consumer)
          .WithWaitStrategy(Wait.ForUnixContainer()
              .UntilMessageIsLogged(consumer.Stdout, "Started"))
          .Build();
  }

  public int Port => _container.GetMappedPublicPort(8081);
  
  public string Host => _container.Hostname;


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

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

  public void Dispose()
  {
      consumer.Dispose();
  }

} `

Together with a DelegatingHandler which rewrites the url: request.RequestUri = new Uri($"https://{_host}:{_portNumber}{request.RequestUri.PathAndQuery}");

ktjn avatar Sep 27 '22 12:09 ktjn

This is the same way I did it with creating the default db and container as well - https://github.com/WestDiscGolf/CosmosDB-Testcontainers-Test/blob/main/tests/Api.Tests/Infrastructure/DataContainerFixture.cs

The key for me was the delegating handler to make sure the updated uri was mapped correctly as the default port seemed hardcoded somewhere in the emulator - https://github.com/WestDiscGolf/CosmosDB-Testcontainers-Test/blob/8c04fd345fc272123f4ac964beaea0d0d690ca41/tests/Api.Tests/Infrastructure/FixRequestLocationHandler.cs

WestDiscGolf avatar Sep 28 '22 08:09 WestDiscGolf

So I've tried the above solution to running CosmosDB emulator but I keep getting the following error:

System.Net.Http.HttpRequestException : No connection could be made because the target machine actively refused it. (localhost:49174)

This exception is thrown when executing the following line: var response = await _client.CreateDatabaseAsync("mydatabase", 4000);

Any ideas? I'm using version 2.2.0-beta.3123645733

wbuck avatar Oct 11 '22 01:10 wbuck

@wbuck Can you add more information about your host setup (OS, Docker version, etc.)? How do you set up your client?

HofmeisterAn avatar Oct 11 '22 05:10 HofmeisterAn

@HofmeisterAn Yeah of course.

The code I'm using to setup the client is identical to what is shown here except that I've exposed more ports in an attempt to actually connect with the container.

I've also tried different addresses (instead of localhost). The code for the FixRequestLocationHandler can be found here.

Like I mentioned previously I copied the code that @WestDiscGolf had shown.

I should also mention that the container is starting successfully, I just can't seem to connect to it. I have tried disabling all firewalls as well to no avail.

[TestFixture]
internal class ProjectServiceTests
{
    private readonly IOutputConsumer _consumer =
        Consume.RedirectStdoutAndStderrToStream(new MemoryStream(), new MemoryStream());

    private const string AccountKey = 
        "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";

    private ITestcontainersContainer _container;
    private CosmosClient _client;

    [OneTimeSetUp]
    public async Task InitializeAsync()
    {
        _container = new TestcontainersBuilder<TestcontainersContainer>()
            .WithImage("mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator")            
            .WithExposedPort(8081)
            .WithExposedPort(8900)
            .WithExposedPort(8901)
            .WithExposedPort(8902)
            .WithExposedPort(10250)
            .WithExposedPort(10251)
            .WithExposedPort(10252)
            .WithExposedPort(10253)
            .WithExposedPort(10254)
            .WithExposedPort(10255)
            .WithExposedPort(10256)
            .WithExposedPort(10350)
            .WithPortBinding(8081, true)
            .WithPortBinding(8900, true)
            .WithPortBinding(8901, true)
            .WithPortBinding(8902, true)
            .WithPortBinding(10250, true)
            .WithPortBinding(10251, true)
            .WithPortBinding(10252, true)
            .WithPortBinding(10253, true)
            .WithPortBinding(10254, true)
            .WithPortBinding(10255, true)
            .WithPortBinding(10256, true)
            .WithPortBinding(10350, true)
            .WithEnvironment("AZURE_COSMOS_EMULATOR_PARTITION_COUNT", "10")
            .WithEnvironment("AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE", "127.0.0.1")
            .WithEnvironment("AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE", "false")
            .WithOutputConsumer(_consumer)
            .WithWaitStrategy(Wait.ForUnixContainer()
                .UntilMessageIsLogged(_consumer.Stdout, "Started"))
            .Build();

        await _container.StartAsync().ConfigureAwait(false);

        var mappedPort = _container.GetMappedPublicPort(8081);
        var updated = $"https://localhost:{mappedPort}";

        var cosmosClientBuilder = new CosmosClientBuilder(updated, AccountKey);
        cosmosClientBuilder.WithHttpClientFactory(() =>
        {
            var handler = new HttpClientHandler
            {
                ServerCertificateCustomValidationCallback = 
                    HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
            };

            return new HttpClient(new FixRequestLocationHandler(mappedPort, handler));
        });

        cosmosClientBuilder.WithConnectionModeGateway();
        _client = cosmosClientBuilder.Build();

        var response = await _client.CreateDatabaseAsync("mydatabase", 4000)
            .ConfigureAwait(false);

        var container = await response.Database.CreateContainerAsync("organization", "/organizationId", 4000)
            .ConfigureAwait(false);
    }

    [OneTimeTearDown]
    public async Task TearDownAsync()
    {
        await _container.StopAsync().ConfigureAwait(false);
    }

    [Test]
    public void DoNothing() 
    {
        Debugger.Break();
    }
}

OS: Windows 10 Enterprise, version 21H2, build 19044.2006 Docker: version 20.10.17, build 100c701 The Linux containers are backed by WSL 2.

wbuck avatar Oct 11 '22 12:10 wbuck

I've also tried different addresses (instead of localhost).

Do not use localhost. Use the Hostname property instead. This applies to FixRequestLocationHandler too. localhost is not always right. Can you double-check if the Hostname property resolves to the correct hostname / ip of the container? Does the IpAddress property work?

HofmeisterAn avatar Oct 11 '22 12:10 HofmeisterAn