Skip to content

Commit

Permalink
chore: Apply PR suggestion to other resource operations
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Sep 5, 2024
1 parent f40eebf commit 13d9202
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 39 deletions.
3 changes: 2 additions & 1 deletion src/Testcontainers/Clients/DockerContainerOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ public async Task<bool> ExistsWithIdAsync(string id, CancellationToken ct = defa
{
try
{
await ByIdAsync(id, ct)
_ = await ByIdAsync(id, ct)
.ConfigureAwait(false);

return true;
}
catch (DockerContainerNotFoundException)
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
4 changes: 2 additions & 2 deletions src/Testcontainers/Containers/DockerContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ await _client.StopAsync(_container.ID, ct)
}
catch (DockerApiException)
{
_container = null;
_container = new ContainerInspectResponse();
}

StoppedTime = DateTime.UtcNow;
Expand All @@ -525,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

0 comments on commit 13d9202

Please sign in to comment.