diff --git a/src/Testcontainers/Clients/DockerContainerOperations.cs b/src/Testcontainers/Clients/DockerContainerOperations.cs index a08a08593..f695d3b23 100644 --- a/src/Testcontainers/Clients/DockerContainerOperations.cs +++ b/src/Testcontainers/Clients/DockerContainerOperations.cs @@ -41,8 +41,9 @@ public async Task ExistsWithIdAsync(string id, CancellationToken ct = defa { try { - await ByIdAsync(id, ct) + _ = await ByIdAsync(id, ct) .ConfigureAwait(false); + return true; } catch (DockerContainerNotFoundException) diff --git a/src/Testcontainers/Clients/DockerImageOperations.cs b/src/Testcontainers/Clients/DockerImageOperations.cs index 5c6c2196b..041cd63e2 100644 --- a/src/Testcontainers/Clients/DockerImageOperations.cs +++ b/src/Testcontainers/Clients/DockerImageOperations.cs @@ -35,26 +35,26 @@ public async Task> GetAllAsync(FilterByProperty } public async Task ByIdAsync(string id, CancellationToken ct = default) + { + return await DockerClient.Images.InspectImageAsync(id, ct) + .ConfigureAwait(false); + } + + public async Task ExistsWithIdAsync(string id, CancellationToken ct = default) { try { - return await DockerClient.Images.InspectImageAsync(id, ct) + _ = await ByIdAsync(id, ct) .ConfigureAwait(false); + + return true; } - catch (DockerApiException) + catch (DockerImageNotFoundException) { - return null; + return false; } } - public async Task ExistsWithIdAsync(string id, CancellationToken ct = default) - { - var response = await ByIdAsync(id, ct) - .ConfigureAwait(false); - - return response != null; - } - public async Task CreateAsync(IImage image, IDockerRegistryAuthenticationConfiguration dockerRegistryAuthConfig, CancellationToken ct = default) { var createParameters = new ImagesCreateParameters diff --git a/src/Testcontainers/Clients/DockerNetworkOperations.cs b/src/Testcontainers/Clients/DockerNetworkOperations.cs index d4a15af04..982ad2822 100644 --- a/src/Testcontainers/Clients/DockerNetworkOperations.cs +++ b/src/Testcontainers/Clients/DockerNetworkOperations.cs @@ -30,26 +30,26 @@ public async Task> GetAllAsync(FilterByProperty fil } public async Task ByIdAsync(string id, CancellationToken ct = default) + { + return await DockerClient.Networks.InspectNetworkAsync(id, ct) + .ConfigureAwait(false); + } + + public async Task ExistsWithIdAsync(string id, CancellationToken ct = default) { try { - return await DockerClient.Networks.InspectNetworkAsync(id, ct) + _ = await ByIdAsync(id, ct) .ConfigureAwait(false); + + return true; } - catch (DockerApiException) + catch (DockerNetworkNotFoundException) { - return null; + return false; } } - public async Task ExistsWithIdAsync(string id, CancellationToken ct = default) - { - var response = await ByIdAsync(id, ct) - .ConfigureAwait(false); - - return response != null; - } - public async Task CreateAsync(INetworkConfiguration configuration, CancellationToken ct = default) { var createParameters = new NetworksCreateParameters diff --git a/src/Testcontainers/Clients/DockerVolumeOperations.cs b/src/Testcontainers/Clients/DockerVolumeOperations.cs index aebd74d71..c04fd42a4 100644 --- a/src/Testcontainers/Clients/DockerVolumeOperations.cs +++ b/src/Testcontainers/Clients/DockerVolumeOperations.cs @@ -34,26 +34,26 @@ public async Task> GetAllAsync(FilterByProperty filt } public async Task ByIdAsync(string id, CancellationToken ct = default) + { + return await DockerClient.Volumes.InspectAsync(id, ct) + .ConfigureAwait(false); + } + + public async Task ExistsWithIdAsync(string id, CancellationToken ct = default) { try { - return await DockerClient.Volumes.InspectAsync(id, ct) + _ = await ByIdAsync(id, ct) .ConfigureAwait(false); + + return true; } catch (DockerApiException) { - return null; + return false; } } - public async Task ExistsWithIdAsync(string id, CancellationToken ct = default) - { - var response = await ByIdAsync(id, ct) - .ConfigureAwait(false); - - return response != null; - } - public async Task CreateAsync(IVolumeConfiguration configuration, CancellationToken ct = default) { var createParameters = new VolumesCreateParameters diff --git a/src/Testcontainers/Clients/TestcontainersClient.cs b/src/Testcontainers/Clients/TestcontainersClient.cs index 1d1090f66..576c51372 100644 --- a/src/Testcontainers/Clients/TestcontainersClient.cs +++ b/src/Testcontainers/Clients/TestcontainersClient.cs @@ -9,6 +9,7 @@ namespace DotNet.Testcontainers.Clients using System.Threading; using System.Threading.Tasks; using Docker.DotNet; + using Docker.DotNet.Models; using DotNet.Testcontainers.Builders; using DotNet.Testcontainers.Configurations; using DotNet.Testcontainers.Containers; @@ -286,6 +287,8 @@ public async Task ReadFileAsync(string id, string filePath, Cancellation /// public async Task RunAsync(IContainerConfiguration configuration, CancellationToken ct = default) { + ImageInspectResponse cachedImage; + if (TestcontainersSettings.ResourceReaperEnabled && ResourceReaper.DefaultSessionId.Equals(configuration.SessionId)) { var isWindowsEngineEnabled = await System.GetIsWindowsEngineEnabled(ct) @@ -295,8 +298,15 @@ public async Task RunAsync(IContainerConfiguration configuration, Cancel .ConfigureAwait(false); } - var cachedImage = await Image.ByIdAsync(configuration.Image.FullName, ct) - .ConfigureAwait(false); + try + { + cachedImage = await Image.ByIdAsync(configuration.Image.FullName, ct) + .ConfigureAwait(false); + } + catch (DockerImageNotFoundException) + { + cachedImage = null; + } if (configuration.ImagePullPolicy(cachedImage)) { @@ -325,8 +335,17 @@ await Task.WhenAll(configuration.ResourceMappings.Select(resourceMapping => Copy /// public async Task BuildAsync(IImageFromDockerfileConfiguration configuration, CancellationToken ct = default) { - var cachedImage = await Image.ByIdAsync(configuration.Image.FullName, ct) - .ConfigureAwait(false); + ImageInspectResponse cachedImage; + + try + { + cachedImage = await Image.ByIdAsync(configuration.Image.FullName, ct) + .ConfigureAwait(false); + } + catch (DockerImageNotFoundException) + { + cachedImage = null; + } if (configuration.ImageBuildPolicy(cachedImage)) { diff --git a/src/Testcontainers/Containers/DockerContainer.cs b/src/Testcontainers/Containers/DockerContainer.cs index b0e1143f0..138beaa5b 100644 --- a/src/Testcontainers/Containers/DockerContainer.cs +++ b/src/Testcontainers/Containers/DockerContainer.cs @@ -515,7 +515,7 @@ await _client.StopAsync(_container.ID, ct) } catch (DockerApiException) { - _container = null; + _container = new ContainerInspectResponse(); } StoppedTime = DateTime.UtcNow; @@ -525,7 +525,7 @@ await _client.StopAsync(_container.ID, ct) /// protected override bool Exists() { - return _container != null && ContainerHasBeenCreatedStates.HasFlag(State); + return ContainerHasBeenCreatedStates.HasFlag(State); } ///