Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Throw exception if Docker resource does not exist instead of silently ignoring it #1254

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/Testcontainers/Clients/DockerContainerOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,26 @@ public async Task<IEnumerable<ContainerListResponse>> GetAllAsync(FilterByProper
}

public async Task<ContainerInspectResponse> ByIdAsync(string id, CancellationToken ct = default)
{
return await DockerClient.Containers.InspectContainerAsync(id, ct)
.ConfigureAwait(false);
}

public async Task<bool> ExistsWithIdAsync(string id, CancellationToken ct = default)
{
try
{
return await DockerClient.Containers.InspectContainerAsync(id, ct)
_ = await ByIdAsync(id, ct)
.ConfigureAwait(false);

return true;
}
catch (DockerApiException)
catch (DockerContainerNotFoundException)
{
return null;
return false;
}
}

public async Task<bool> ExistsWithIdAsync(string id, CancellationToken ct = default)
{
var response = await ByIdAsync(id, ct)
.ConfigureAwait(false);

return response != null;
}

public async Task<long> GetExitCodeAsync(string id, CancellationToken ct = default)
{
var response = await DockerClient.Containers.WaitContainerAsync(id, ct)
Expand Down
22 changes: 11 additions & 11 deletions src/Testcontainers/Clients/DockerImageOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,26 @@ public async Task<IEnumerable<ImagesListResponse>> GetAllAsync(FilterByProperty
}

public async Task<ImageInspectResponse> ByIdAsync(string id, CancellationToken ct = default)
{
return await DockerClient.Images.InspectImageAsync(id, ct)
.ConfigureAwait(false);
}

public async Task<bool> 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<bool> 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
Expand Down
22 changes: 11 additions & 11 deletions src/Testcontainers/Clients/DockerNetworkOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,26 @@ public async Task<IEnumerable<NetworkResponse>> GetAllAsync(FilterByProperty fil
}

public async Task<NetworkResponse> ByIdAsync(string id, CancellationToken ct = default)
{
return await DockerClient.Networks.InspectNetworkAsync(id, ct)
.ConfigureAwait(false);
}

public async Task<bool> 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<bool> ExistsWithIdAsync(string id, CancellationToken ct = default)
{
var response = await ByIdAsync(id, ct)
.ConfigureAwait(false);

return response != null;
}

public async Task<string> CreateAsync(INetworkConfiguration configuration, CancellationToken ct = default)
{
var createParameters = new NetworksCreateParameters
Expand Down
20 changes: 10 additions & 10 deletions src/Testcontainers/Clients/DockerVolumeOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,26 @@ public async Task<IEnumerable<VolumeResponse>> GetAllAsync(FilterByProperty filt
}

public async Task<VolumeResponse> ByIdAsync(string id, CancellationToken ct = default)
{
return await DockerClient.Volumes.InspectAsync(id, ct)
.ConfigureAwait(false);
}

public async Task<bool> 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<bool> ExistsWithIdAsync(string id, CancellationToken ct = default)
{
var response = await ByIdAsync(id, ct)
.ConfigureAwait(false);

return response != null;
}

public async Task<string> CreateAsync(IVolumeConfiguration configuration, CancellationToken ct = default)
{
var createParameters = new VolumesCreateParameters
Expand Down
27 changes: 23 additions & 4 deletions src/Testcontainers/Clients/TestcontainersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -286,6 +287,8 @@ public async Task<byte[]> ReadFileAsync(string id, string filePath, Cancellation
/// <inheritdoc />
public async Task<string> RunAsync(IContainerConfiguration configuration, CancellationToken ct = default)
{
ImageInspectResponse cachedImage;

if (TestcontainersSettings.ResourceReaperEnabled && ResourceReaper.DefaultSessionId.Equals(configuration.SessionId))
{
var isWindowsEngineEnabled = await System.GetIsWindowsEngineEnabled(ct)
Expand All @@ -295,8 +298,15 @@ public async Task<string> 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))
{
Expand Down Expand Up @@ -325,8 +335,17 @@ await Task.WhenAll(configuration.ResourceMappings.Select(resourceMapping => Copy
/// <inheritdoc />
public async Task<string> 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))
{
Expand Down
14 changes: 11 additions & 3 deletions src/Testcontainers/Containers/DockerContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace DotNet.Testcontainers.Containers
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Docker.DotNet;
using Docker.DotNet.Models;
using DotNet.Testcontainers.Clients;
using DotNet.Testcontainers.Configurations;
Expand Down Expand Up @@ -507,8 +508,15 @@ protected virtual async Task UnsafeStopAsync(CancellationToken ct = default)
await _client.StopAsync(_container.ID, ct)
.ConfigureAwait(false);

_container = await _client.Container.ByIdAsync(_container.ID, ct)
.ConfigureAwait(false);
try
{
_container = await _client.Container.ByIdAsync(_container.ID, ct)
.ConfigureAwait(false);
}
catch (DockerContainerNotFoundException)
{
_container = new ContainerInspectResponse();
}

StoppedTime = DateTime.UtcNow;
Stopped?.Invoke(this, EventArgs.Empty);
Expand All @@ -517,7 +525,7 @@ await _client.StopAsync(_container.ID, ct)
/// <inheritdoc />
protected override bool Exists()
{
return _container != null && ContainerHasBeenCreatedStates.HasFlag(State);
return ContainerHasBeenCreatedStates.HasFlag(State);
}

/// <summary>
Expand Down
Loading