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 1 commit
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
21 changes: 10 additions & 11 deletions src/Testcontainers/Clients/DockerContainerOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,25 @@ 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
12 changes: 10 additions & 2 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 (DockerApiException)
{
_container = null;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I think this should be:

Suggested change
catch (DockerApiException)
{
_container = null;
}
catch (DockerApiException)
{
_container = new ContainerInspectResponse();
}

We can apply the same changes to the image, network and volume operation classes. I can do it in a follow-up PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think setting to null is correct (it was the previous behavior) because the Exists method just below checks _container != null.

We can apply the same changes to the image, network and volume operation classes. I can do it in a follow-up PR.

I tried this but it needs some more adaptation too: https://github.com/testcontainers/testcontainers-dotnet/runs/29692350906

Copy link
Collaborator

@HofmeisterAn HofmeisterAn Sep 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember it was like that before: 6e31cf3. IIRC it was/is not supposed to be null (and with your change we do not set it to null again). Even in UnsafeDeleteAsync(CancellationToken) we set it to an instance.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this but it needs some more adaptation too: https://github.com/testcontainers/testcontainers-dotnet/runs/29692350906

This is based on how we determine if an image already exists and whether we need to pull it. I updated the PR, LMKWYT. I will merge it if you are good with the changes


StoppedTime = DateTime.UtcNow;
Stopped?.Invoke(this, EventArgs.Empty);
Expand Down
Loading