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

chore: Remove unnecessary internal APIs #1020

Merged
merged 1 commit into from
Oct 11, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ContainerConfigurationConverter(IContainerConfiguration configuration)

private static string GetQualifiedPort(string containerPort)
{
return new[] { UdpPortSuffix, TcpPortSuffix, SctpPortSuffix }.Any(portSuffix => containerPort.EndsWith(portSuffix, StringComparison.OrdinalIgnoreCase)) ? containerPort.ToLowerInvariant() : containerPort + TcpPortSuffix;
return Array.Exists(new[] { UdpPortSuffix, TcpPortSuffix, SctpPortSuffix }, portSuffix => containerPort.EndsWith(portSuffix, StringComparison.OrdinalIgnoreCase)) ? containerPort.ToLowerInvariant() : containerPort + TcpPortSuffix;
}

private sealed class ToCollection : CollectionConverter<string, string>
Expand Down
26 changes: 5 additions & 21 deletions src/Testcontainers/Clients/DockerContainerOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,11 @@ public async Task<IEnumerable<ContainerListResponse>> GetAllAsync(FilterByProper
.ConfigureAwait(false);
}

public Task<ContainerInspectResponse> ByIdAsync(string id, CancellationToken ct = default)
{
return ByPropertyAsync("id", id, ct);
}

public Task<ContainerInspectResponse> ByNameAsync(string name, CancellationToken ct = default)
{
return ByPropertyAsync("name", name, ct);
}

public async Task<ContainerInspectResponse> ByPropertyAsync(string property, string value, CancellationToken ct = default)
public async Task<ContainerInspectResponse> ByIdAsync(string id, CancellationToken ct = default)
{
try
{
return await Docker.Containers.InspectContainerAsync(value, ct)
return await Docker.Containers.InspectContainerAsync(id, ct)
.ConfigureAwait(false);
}
catch (DockerApiException)
Expand All @@ -65,18 +55,12 @@ public async Task<bool> ExistsWithIdAsync(string id, CancellationToken ct = defa
return response != null;
}

public async Task<bool> ExistsWithNameAsync(string name, CancellationToken ct = default)
public async Task<long> GetExitCodeAsync(string id, CancellationToken ct = default)
{
var response = await ByNameAsync(name, ct)
var response = await Docker.Containers.WaitContainerAsync(id, ct)
.ConfigureAwait(false);

return response != null;
}

public async Task<long> GetExitCodeAsync(string id, CancellationToken ct = default)
{
return (await Docker.Containers.WaitContainerAsync(id, ct)
.ConfigureAwait(false)).StatusCode;
return response.StatusCode;
}

public async Task<(string Stdout, string Stderr)> GetLogsAsync(string id, TimeSpan since, TimeSpan until, bool timestampsEnabled = true, CancellationToken ct = default)
Expand Down
26 changes: 4 additions & 22 deletions src/Testcontainers/Clients/DockerImageOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,11 @@ public async Task<IEnumerable<ImagesListResponse>> GetAllAsync(FilterByProperty
.ConfigureAwait(false);
}

public Task<ImageInspectResponse> ByIdAsync(string id, CancellationToken ct = default)
{
return ByPropertyAsync("id", id, ct);
}

public Task<ImageInspectResponse> ByNameAsync(string name, CancellationToken ct = default)
{
return ByPropertyAsync("reference", name, ct);
}

public async Task<ImageInspectResponse> ByPropertyAsync(string property, string value, CancellationToken ct = default)
public async Task<ImageInspectResponse> ByIdAsync(string id, CancellationToken ct = default)
{
try
{
return await Docker.Images.InspectImageAsync(value, ct)
return await Docker.Images.InspectImageAsync(id, ct)
.ConfigureAwait(false);
}
catch (DockerApiException)
Expand All @@ -68,14 +58,6 @@ public async Task<bool> ExistsWithIdAsync(string id, CancellationToken ct = defa
return response != null;
}

public async Task<bool> ExistsWithNameAsync(string name, CancellationToken ct = default)
{
var response = await ByNameAsync(name, ct)
.ConfigureAwait(false);

return response != null;
}

public async Task CreateAsync(IImage image, IDockerRegistryAuthenticationConfiguration dockerRegistryAuthConfig, CancellationToken ct = default)
{
var createParameters = new ImagesCreateParameters
Expand Down Expand Up @@ -108,7 +90,7 @@ public async Task<string> BuildAsync(IImageFromDockerfileConfiguration configura
{
var image = configuration.Image;

var imageExists = await ExistsWithNameAsync(image.FullName, ct)
var imageExists = await ExistsWithIdAsync(image.FullName, ct)
.ConfigureAwait(false);

if (imageExists && configuration.DeleteIfExists.HasValue && configuration.DeleteIfExists.Value)
Expand Down Expand Up @@ -143,7 +125,7 @@ await DeleteAsync(image, ct)
await Docker.Images.BuildImageFromDockerfileAsync(buildParameters, dockerfileArchiveStream, Array.Empty<AuthConfig>(), new Dictionary<string, string>(), _traceProgress, ct)
.ConfigureAwait(false);

var imageHasBeenCreated = await ExistsWithNameAsync(image.FullName, ct)
var imageHasBeenCreated = await ExistsWithIdAsync(image.FullName, ct)
.ConfigureAwait(false);

if (!imageHasBeenCreated)
Expand Down
22 changes: 2 additions & 20 deletions src/Testcontainers/Clients/DockerNetworkOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,11 @@ public async Task<IEnumerable<NetworkResponse>> GetAllAsync(FilterByProperty fil
.ConfigureAwait(false);
}

public Task<NetworkResponse> ByIdAsync(string id, CancellationToken ct = default)
{
return ByPropertyAsync("id", id, ct);
}

public Task<NetworkResponse> ByNameAsync(string name, CancellationToken ct = default)
{
return ByPropertyAsync("name", name, ct);
}

public async Task<NetworkResponse> ByPropertyAsync(string property, string value, CancellationToken ct = default)
public async Task<NetworkResponse> ByIdAsync(string id, CancellationToken ct = default)
{
try
{
return await Docker.Networks.InspectNetworkAsync(value, ct)
return await Docker.Networks.InspectNetworkAsync(id, ct)
.ConfigureAwait(false);
}
catch (DockerApiException)
Expand All @@ -63,14 +53,6 @@ public async Task<bool> ExistsWithIdAsync(string id, CancellationToken ct = defa
return response != null;
}

public async Task<bool> ExistsWithNameAsync(string name, CancellationToken ct = default)
{
var response = await ByNameAsync(name, ct)
.ConfigureAwait(false);

return response != null;
}

public async Task<string> CreateAsync(INetworkConfiguration configuration, CancellationToken ct = default)
{
var createParameters = new NetworksCreateParameters
Expand Down
1 change: 1 addition & 0 deletions src/Testcontainers/Clients/DockerSystemOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public async Task<bool> GetIsWindowsEngineEnabled(CancellationToken ct = default
{
var version = await GetVersionAsync(ct)
.ConfigureAwait(false);

return version.Os.IndexOf("Windows", StringComparison.OrdinalIgnoreCase) > -1;
}

Expand Down
22 changes: 2 additions & 20 deletions src/Testcontainers/Clients/DockerVolumeOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,11 @@ public async Task<IEnumerable<VolumeResponse>> GetAllAsync(FilterByProperty filt
return response.Volumes;
}

public Task<VolumeResponse> ByIdAsync(string id, CancellationToken ct = default)
{
return ByPropertyAsync("id", id, ct);
}

public Task<VolumeResponse> ByNameAsync(string name, CancellationToken ct = default)
{
return ByPropertyAsync("name", name, ct);
}

public async Task<VolumeResponse> ByPropertyAsync(string property, string value, CancellationToken ct = default)
public async Task<VolumeResponse> ByIdAsync(string id, CancellationToken ct = default)
{
try
{
return await Docker.Volumes.InspectAsync(value, ct)
return await Docker.Volumes.InspectAsync(id, ct)
.ConfigureAwait(false);
}
catch (DockerApiException)
Expand All @@ -67,14 +57,6 @@ public async Task<bool> ExistsWithIdAsync(string id, CancellationToken ct = defa
return response != null;
}

public async Task<bool> ExistsWithNameAsync(string name, CancellationToken ct = default)
{
var response = await ByNameAsync(name, ct)
.ConfigureAwait(false);

return response != null;
}

public async Task<string> CreateAsync(IVolumeConfiguration configuration, CancellationToken ct = default)
{
var createParameters = new VolumesCreateParameters
Expand Down
6 changes: 0 additions & 6 deletions src/Testcontainers/Clients/IHasListOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ internal interface IHasListOperations<TListEntity, TInspectEntity>

Task<TInspectEntity> ByIdAsync(string id, CancellationToken ct = default);

Task<TInspectEntity> ByNameAsync(string name, CancellationToken ct = default);

Task<TInspectEntity> ByPropertyAsync(string property, string value, CancellationToken ct = default);

Task<bool> ExistsWithIdAsync(string id, CancellationToken ct = default);

Task<bool> ExistsWithNameAsync(string name, CancellationToken ct = default);
}
}
4 changes: 2 additions & 2 deletions src/Testcontainers/Clients/TestcontainersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public async Task<string> RunAsync(IContainerConfiguration configuration, Cancel
.ConfigureAwait(false);
}

var cachedImage = await Image.ByNameAsync(configuration.Image.FullName, ct)
var cachedImage = await Image.ByIdAsync(configuration.Image.FullName, ct)
.ConfigureAwait(false);

if (configuration.ImagePullPolicy(cachedImage))
Expand Down Expand Up @@ -323,7 +323,7 @@ await Task.WhenAll(configuration.ResourceMappings.Values.Select(resourceMapping
/// <inheritdoc />
public async Task<string> BuildAsync(IImageFromDockerfileConfiguration configuration, CancellationToken ct = default)
{
var cachedImage = await Image.ByNameAsync(configuration.Image.FullName, ct)
var cachedImage = await Image.ByIdAsync(configuration.Image.FullName, ct)
.ConfigureAwait(false);

if (configuration.ImageBuildPolicy(cachedImage))
Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers/Images/FutureDockerImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ protected override async Task UnsafeCreateAsync(CancellationToken ct = default)
_ = await _client.BuildAsync(_configuration, ct)
.ConfigureAwait(false);

_image = await _client.Image.ByNameAsync(_configuration.Image.FullName, ct)
_image = await _client.Image.ByIdAsync(_configuration.Image.FullName, ct)
.ConfigureAwait(false);
}

Expand Down
33 changes: 17 additions & 16 deletions src/Testcontainers/Images/MatchImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,33 @@ namespace DotNet.Testcontainers.Images

internal static class MatchImage
{
private static readonly char[] SlashSeparator = { '/' };

private static readonly char[] ColonSeparator = { ':' };

public static IImage Match(string image)
{
_ = Guard.Argument(image, nameof(image))
.NotNull()
.NotEmpty();

var imageComponents = image
.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
var imageComponents = image.Split(SlashSeparator, StringSplitOptions.RemoveEmptyEntries);

var registry = string.Join("/", imageComponents.Take(imageComponents.Length - 1));

var repository = string.Join("/", imageComponents
.Take(imageComponents.Length - 1));
imageComponents = imageComponents[imageComponents.Length - 1].Split(ColonSeparator, StringSplitOptions.RemoveEmptyEntries);

var name = imageComponents
.Last()
.Split(':')
.DefaultIfEmpty(string.Empty)
.First();
if (2.Equals(imageComponents.Length))
{
return new DockerImage(registry, imageComponents[0], imageComponents[1]);
}

var tag = imageComponents
.Last()
.Split(':')
.Skip(1)
.DefaultIfEmpty(string.Empty)
.First();
if (1.Equals(imageComponents.Length))
{
return new DockerImage(registry, imageComponents[0], string.Empty);
}

return new DockerImage(repository, name, tag);
throw new ArgumentException("Cannot parse image: " + image, nameof(image));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,6 @@ public async Task QueryNotExistingDockerVolumeById()
.ConfigureAwait(false));
}

[Fact]
public async Task QueryNotExistingDockerContainerByName()
{
Assert.False(await Client.Container.ExistsWithNameAsync(ResourceIdOrName)
.ConfigureAwait(false));
}

[Fact]
public async Task QueryNotExistingDockerImageByName()
{
Assert.False(await Client.Image.ExistsWithNameAsync(ResourceIdOrName)
.ConfigureAwait(false));
}

[Fact]
public async Task QueryNotExistingDockerNetworkByName()
{
Assert.False(await Client.Network.ExistsWithNameAsync(ResourceIdOrName)
.ConfigureAwait(false));
}

[Fact]
public async Task QueryNotExistingDockerVolumeByName()
{
Assert.False(await Client.Volume.ExistsWithNameAsync(ResourceIdOrName)
.ConfigureAwait(false));
}

[Fact]
public async Task QueryContainerInformationOfCreatedContainer()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public async Task CreateNetworkAssignsOptions()
var client = new TestcontainersClient(ResourceReaper.DefaultSessionId, TestcontainersSettings.OS.DockerEndpointAuthConfig, NullLogger.Instance);

// When
var networkResponse = await client.Network.ByNameAsync(_network.Name)
var networkResponse = await client.Network.ByIdAsync(_network.Name)
.ConfigureAwait(false);

// Then
Expand All @@ -66,7 +66,7 @@ public async Task CreateNetworkAssignsLabels()
var client = new TestcontainersClient(ResourceReaper.DefaultSessionId, TestcontainersSettings.OS.DockerEndpointAuthConfig, NullLogger.Instance);

// When
var networkResponse = await client.Network.ByNameAsync(_network.Name)
var networkResponse = await client.Network.ByIdAsync(_network.Name)
.ConfigureAwait(false);

// Then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public async Task CreateVolumeAssignsLabels()
var client = new TestcontainersClient(ResourceReaper.DefaultSessionId, TestcontainersSettings.OS.DockerEndpointAuthConfig, NullLogger.Instance);

// When
var volumeResponse = await client.Volume.ByNameAsync(_volume.Name)
var volumeResponse = await client.Volume.ByIdAsync(_volume.Name)
.ConfigureAwait(false);

// Then
Expand Down