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.73k stars 262 forks source link

[Bug]: DotNet.Testcontainers.Containers.ResourceReaperException : Initialization has been cancelled. #1222

Closed azan-n closed 1 month ago

azan-n commented 1 month ago

Testcontainers version

3.9.0

Using the latest Testcontainers version?

Yes

Host OS

macOS

Host arch

arm64

.NET version

8.0

Docker version

Client: Docker Engine - Community
 Version:           26.1.4
 API version:       1.43 (downgraded from 1.45)
 Go version:        go1.22.4
 Git commit:        5650f9b102
 Built:             Wed Jun  5 10:47:13 2024
 OS/Arch:           darwin/arm64
 Context:           colima

Server: Docker Engine - Community
 Engine:
  Version:          24.0.9
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.13
  Git commit:       fca702d
  Built:            Thu Feb  1 00:48:55 2024
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          1.6.28
  GitCommit:        ae07eda36dd25f8a1b98dfbf587313b99c0190bb
 runc:
  Version:          1.1.12
  GitCommit:        v1.1.12-0-g51d5e94
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Docker info

Client: Docker Engine - Community
 Version:    26.1.4
 Context:    colima
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.14.1
    Path:     /opt/homebrew/lib/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  2.27.1
    Path:     /opt/homebrew/lib/docker/cli-plugins/docker-compose

Server:
 Containers: 81
  Running: 0
  Paused: 0
  Stopped: 81
 Images: 171
 Server Version: 24.0.9
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: ae07eda36dd25f8a1b98dfbf587313b99c0190bb
 runc version: v1.1.12-0-g51d5e94
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: builtin
  cgroupns
 Kernel Version: 6.5.0-15-generic
 Operating System: Ubuntu 23.10
 OSType: linux
 Architecture: aarch64
 CPUs: 2
 Total Memory: 1.851GiB
 Name: colima
 ID: 0a11c8a0-a432-485b-9b95-19e787f094f6
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

What happened?

Created a new project to try testcontainers for integration testing. The project has one test that only uses the WebApplicationFactory, a very minimal setup for it to be anything else.

public class WebApiFactory : WebApplicationFactory<Program>, IAsyncLifetime
{
    private PostgreSqlContainer database = new PostgreSqlBuilder()
            .WithImage("postgres:14.12")
            .Build();

    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        builder.ConfigureTestServices(services =>
        {
            // ...
        });
    }

    public async Task InitializeAsync()
    {
        await database.StartAsync();
    }

    public new async Task DisposeAsync()
    {
        await database.DisposeAsync();
    }
}

The test suite does not error (at least not due to testcontainers) after calling the following:

TestcontainersSettings.ResourceReaperEnabled = false;

Relevant log output

[xUnit.net 00:01:00.59]     WebApi.IntegrationTests.IntegrationTests.Signup [FAIL]
  Failed WebApi.IntegrationTests.IntegrationTests.Signup [1 ms]
  Error Message:
   DotNet.Testcontainers.Containers.ResourceReaperException : Initialization has been cancelled.
  Stack Trace:
     at DotNet.Testcontainers.Containers.ResourceReaper.GetAndStartNewAsync(Guid sessionId, IDockerEndpointAuthenticationConfiguration dockerEndpointAuthConfig, IImage resourceReaperImage, IMount dockerSocket, ILogger logger, Boolean requiresPrivilegedMode, TimeSpan initTimeout, CancellationToken ct) in /_/src/Testcontainers/Containers/ResourceReaper.cs:line 231
   at DotNet.Testcontainers.Containers.ResourceReaper.GetAndStartNewAsync(Guid sessionId, IDockerEndpointAuthenticationConfiguration dockerEndpointAuthConfig, IImage resourceReaperImage, IMount dockerSocket, ILogger logger, Boolean requiresPrivilegedMode, TimeSpan initTimeout, CancellationToken ct) in /_/src/Testcontainers/Containers/ResourceReaper.cs:line 243
   at DotNet.Testcontainers.Containers.ResourceReaper.GetAndStartDefaultAsync(IDockerEndpointAuthenticationConfiguration dockerEndpointAuthConfig, ILogger logger, Boolean isWindowsEngineEnabled, CancellationToken ct) in /_/src/Testcontainers/Containers/ResourceReaper.cs:line 135
   at DotNet.Testcontainers.Clients.TestcontainersClient.RunAsync(IContainerConfiguration configuration, CancellationToken ct) in /_/src/Testcontainers/Clients/TestcontainersClient.cs:line 294
   at DotNet.Testcontainers.Containers.DockerContainer.UnsafeCreateAsync(CancellationToken ct) in /_/src/Testcontainers/Containers/DockerContainer.cs:line 413
   at DotNet.Testcontainers.Containers.DockerContainer.StartAsync(CancellationToken ct) in /_/src/Testcontainers/Containers/DockerContainer.cs:line 277
   at WebApi.IntegrationTests.WebApiFactory.InitializeAsync() in /Users/user/Documents/Projects/web-api/WebApi.IntegrationTests/Utilities/Fixtures.cs:line 53

Additional information

The WebApplicationFactory is supposed to be used through a ICollectionFixture instead of the more conventionally used IClassFixture.

HofmeisterAn commented 1 month ago

This is not a bug. You need to configure your Colima environment properly. There are plenty of similar questions regarding this topic. Please use the search function. If you need further help, let me know. I can assist as soon as I am back from vacation.

azan-n commented 1 month ago

Setting the CollectionDefinition with DisableParallelization = true as suggested here leads to

rror Message:
   System.NullReferenceException : Object reference not set to an instance of an object.
  Stack Trace:
     at DotNet.Testcontainers.Containers.DockerContainer.WaitUntilPortBindingsMapped.UntilAsync(IContainer _, CancellationToken ct) in /_/src/Testcontainers/Containers/DockerContainer.cs:line 570
   at DotNet.Testcontainers.Containers.DockerContainer.CheckReadinessAsync(WaitStrategy waitStrategy, CancellationToken ct) in /_/src/Testcontainers/Containers/DockerContainer.cs:line 532
   at DotNet.Testcontainers.Configurations.WaitStrategy.<>c__DisplayClass24_0.<<WaitUntilAsync>g__UntilAsync|0>d.MoveNext() in /_/src/Testcontainers/Configurations/WaitStrategies/WaitStrategy.cs:line 184
--- End of stack trace from previous location ---
   at DotNet.Testcontainers.Configurations.WaitStrategy.WaitUntilAsync(Func`1 wait, TimeSpan interval, TimeSpan timeout, Int32 retries, CancellationToken ct) in /_/src/Testcontainers/Configurations/WaitStrategies/WaitStrategy.cs:line 213
   at DotNet.Testcontainers.Containers.DockerContainer.CheckReadinessAsync(IEnumerable`1 waitStrategies, CancellationToken ct) in /_/src/Testcontainers/Containers/DockerContainer.cs:line 550
   at DotNet.Testcontainers.Containers.DockerContainer.UnsafeStartAsync(CancellationToken ct) in /_/src/Testcontainers/Containers/DockerContainer.cs:line 467
   at DotNet.Testcontainers.Containers.DockerContainer.StartAsync(CancellationToken ct) in /_/src/Testcontainers/Containers/DockerContainer.cs:line 280
   at DotNet.Testcontainers.Containers.ResourceReaper.GetAndStartNewAsync(Guid sessionId, IDockerEndpointAuthenticationConfiguration dockerEndpointAuthConfig, IImage resourceReaperImage, IMount dockerSocket, ILogger logger, Boolean requiresPrivilegedMode, TimeSpan initTimeout, CancellationToken ct) in /_/src/Testcontainers/Containers/ResourceReaper.cs:line 219
   at DotNet.Testcontainers.Containers.ResourceReaper.GetAndStartNewAsync(Guid sessionId, IDockerEndpointAuthenticationConfiguration dockerEndpointAuthConfig, IImage resourceReaperImage, IMount dockerSocket, ILogger logger, Boolean requiresPrivilegedMode, TimeSpan initTimeout, CancellationToken ct) in /_/src/Testcontainers/Containers/ResourceReaper.cs:line 243
   at DotNet.Testcontainers.Containers.ResourceReaper.GetAndStartDefaultAsync(IDockerEndpointAuthenticationConfiguration dockerEndpointAuthConfig, ILogger logger, Boolean isWindowsEngineEnabled, CancellationToken ct) in /_/src/Testcontainers/Containers/ResourceReaper.cs:line 135
   at DotNet.Testcontainers.Clients.TestcontainersClient.RunAsync(IContainerConfiguration configuration, CancellationToken ct) in /_/src/Testcontainers/Clients/TestcontainersClient.cs:line 294
   at DotNet.Testcontainers.Containers.DockerContainer.UnsafeCreateAsync(CancellationToken ct) in /_/src/Testcontainers/Containers/DockerContainer.cs:line 413
   at DotNet.Testcontainers.Containers.DockerContainer.StartAsync(CancellationToken ct) in /_/src/Testcontainers/Containers/DockerContainer.cs:line 277
   at CryptlexWebApi.IntegrationTests.CryptlexWebApiFactory.InitializeAsync()

I'm guessing I should try working with https://dotnet.testcontainers.org/examples/dind/

HofmeisterAn commented 1 month ago

Unfortunately, I do not remember which steps are necessary, and I maybe mixed them up with the issues related to Podman. Nonetheless, using other container runtimes besides Docker requires additional configurations. Since I do not have access to a Mac, I am unable to update the documentation accordingly. However, the Testcontainers for Go docs (https://github.com/testcontainers/testcontainers-dotnet/discussions/689) might provide some guidance. Please refer to the documentation at:

If you're using an older version of Colima or have other applications that are unaware of Docker context the following workaround is available:

Testcontainers for .NET currently lacks the ability to resolve the context. Additionally, please note the following limitation with Colima: https://github.com/testcontainers/testcontainers-dotnet/issues/1085#issuecomment-1881561417. Hope that helps.

azan-n commented 1 month ago

This was a part of an urgent POC. Switching from Colima to Docker Desktop worked.