testcontainers / testcontainers-dotnet

A library to support tests with throwaway instances of Docker containers for all compatible .NET Standard versions.
https://dotnet.testcontainers.org
MIT License
3.65k stars 250 forks source link

[Bug]: Test got stucked after execution of container.startAsync() #1098

Closed bveeni closed 5 months ago

bveeni commented 5 months ago

Testcontainers version

3.5.0

Using the latest Testcontainers version?

Yes

Host OS

Windows

Host arch

64-bit operating system, x64-based processor

.NET version

8

Docker version

24.

Docker info

warn: Microsoft.AspNetCore.Hosting.Diagnostics[15]
      Overriding HTTP_PORTS '8080' and HTTPS_PORTS ''. Binding to values defined by URLS instead 'https://+443'.
warn: Microsoft.AspNetCore.Server.Kestrel[0]
      Overriding address(es) 'https://+443'. Binding to endpoints defined via IConfiguration and/or UseKestrel() instead.
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://[::]:5001
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /app

What happened?

I am trying to run test for my web app running in https. When debugging test, my code is able to create image and start container. I am able to access my web app via browser. The problem is once it passes this line await container.StartAsync(); it get stucked and doesnot go to further code. I couldn't find any relative information regarding this.

However I am able to run it for http. Following is for http which works fine and proceed to next code line after start of container.

 var image = new ImageFromDockerfileBuilder()
      .WithDockerfileDirectory(CommonDirectoryPath.GetSolutionDirectory(), string.Empty)      
      .WithDockerfile("Dockerfile")
      .WithCleanUp(true)

      .Build();

  // create image from Dockerfile
  await image.CreateAsync();

  container = new ContainerBuilder()
      .WithImage(image)
      .WithPortBinding(80, assignRandomHostPort: true)
      // use environment variables to add configuration options
      .WithEnvironment("ASPNETCORE_URLS", "http://+:80")
      .WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(r => r.ForPath("/")))           
      .Build();

  // build container
  await container.StartAsync();
Console.Writeline("Success");

Here is my code snippet for https, which get stucked after executing code of starting container.

var image = new ImageFromDockerfileBuilder()
       .WithDockerfileDirectory(CommonDirectoryPath.GetSolutionDirectory(), string.Empty)

       .WithDockerfile("Dockerfile")
       .WithCleanUp(true)
       .Build();

   // create image from Dockerfile
   await image.CreateAsync();

   container = new ContainerBuilder()
       .WithImage(image)     
       .WithPortBinding(443, true)
       .WithEnvironment("ASPNETCORE_URLS", "https://+443")
       .WithEnvironment("ASPNETCORE_Kestrel__Certificates__Default__Path", "mycertificate.pfx")
       .WithEnvironment("ASPNETCORE_Kestrel__Certificates__Default__Password", "***")
       .WithResourceMapping(new FileInfo("C:\\SNAC_Project\\TestingWithContainers\\TestingWithContainers.WebApp\\appsettings.json"), "/app/")
       .WithBindMount("C:\\SNAC_Project\\Document\\certificates\\mycertificate.pfx", "/app/mycertificate.pfx")
       .WithExposedPort(443)
       .WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(r => r.ForPort(443)))
       .Build();

   // build container 
   await container.StartAsync();
   Console.WriteLine("Success");

Could you please let me know what might went wrong or anything I missed configuring. Thanks

Relevant log output

No response

Additional information

No response

HofmeisterAn commented 5 months ago

This is very likely an issue with the wait strategy and its configuration. I am pretty sure the wait strategy never indicates readiness (success), and Testcontainers keeps trying it over and over again. Can you please check if the app inside the container really starts on 443? According to the logs, it starts on 5001.

Now listening on: https://[::]:5001

If it starts on the right port, make sure the HTTP client is able to verify the certificate.

bveeni commented 5 months ago

Thank you for the reply. Configuring with wait strategy worked for me. Cheers