Skip to content

Support Testcontainers for resource definition #3105

@eddumelendez

Description

@eddumelendez

Hi, I landed in the project by looking at this issue to support Testcontainers for component tests.

I currently see this is how resources are provisioned:

For Redis container:

builder.AddRedis("basketcache")
    .WithDataVolume()
    .WithRedisCommander();

For Azure Redis:

builder.AddRedis("cache").AsAzureRedis()

Proposal

Provide an API to provide specific Resource definitions, let's say AspireResource. For the Redis example, it can use AzureRedisExtensions or Testcontainers Redis module. The API can be also used in case there are other ways to provision a Redis instance, so, developers can write their own implementation.

For Testcontainers Redis:

var redisContainer = new RedisBuilder()
  .WithImage("redis:7.0")
  .Build();

builder.AddRedis("cache", new TestcontainersAspireResource().WithContainer(redisContainer).Build()))

where TestcontainersAspireResource will start the container.

For Azure Redis:

builder.AddRedis("cache", new AzureAspireResource().WithExtension(new AzureRedisExtension()).Build())

Benefits

  • Unify the experience for dev and test by using the same API to provide resources
  • Generic API to define and start a container
  • Support Docker network
  • Integrates with Testcontainers Desktop
  • Docker socket auto-detection
  • Automatic resource cleanup
  • Recently, reuse containers feature was added. So, the same instance can be shared cross-services

Also, by looking at the issues, some of them are already handled by Testcontainers. For example:

All Testcontainers .NET are listed in the module catalog.

If there is no specific module the Testcontainers API is very fluent and allow the user to write their own definition.

var container = new ContainerBuilder()
  .WithImage("testcontainers/helloworld:1.1.0")
  .WithPortBinding(8080, true)
  .WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(r => r.ForPort(8080)))
  .Build();

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

References

In the Java world, this integration has been done in the three major frameworks (Quarkus devservices, Micronaut test-resources, Spring Boot service connection). I would be very happy to chat and share more about it in order to find a similar experience in .NET ecosystem with Aspire.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-integrationsIssues pertaining to Aspire Integrations packages

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions