From b65572d8edda45758bc80c9621ff9a7a2a31fdfd Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Wed, 11 Oct 2023 19:14:36 +0200 Subject: [PATCH] chore: Remove unnecessary internal APIs --- .../ContainerConfigurationConverter.cs | 2 +- .../Clients/DockerContainerOperations.cs | 26 +++------------ .../Clients/DockerImageOperations.cs | 26 +++------------ .../Clients/DockerNetworkOperations.cs | 22 ++----------- .../Clients/DockerSystemOperations.cs | 1 + .../Clients/DockerVolumeOperations.cs | 22 ++----------- .../Clients/IHasListOperations.cs | 6 ---- .../Clients/TestcontainersClient.cs | 4 +-- .../Images/FutureDockerImage.cs | 2 +- src/Testcontainers/Images/MatchImage.cs | 33 ++++++++++--------- .../Configurations/ResourcePropertiesTest.cs | 28 ---------------- .../TestcontainerNetworkBuilderTest.cs | 4 +-- .../TestcontainersVolumeBuilderTest.cs | 2 +- 13 files changed, 38 insertions(+), 140 deletions(-) diff --git a/src/Testcontainers/Clients/ContainerConfigurationConverter.cs b/src/Testcontainers/Clients/ContainerConfigurationConverter.cs index 7a2736117..84f7dfb7c 100644 --- a/src/Testcontainers/Clients/ContainerConfigurationConverter.cs +++ b/src/Testcontainers/Clients/ContainerConfigurationConverter.cs @@ -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> diff --git a/src/Testcontainers/Clients/DockerContainerOperations.cs b/src/Testcontainers/Clients/DockerContainerOperations.cs index de7526acc..33d7e7d9e 100644 --- a/src/Testcontainers/Clients/DockerContainerOperations.cs +++ b/src/Testcontainers/Clients/DockerContainerOperations.cs @@ -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) @@ -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) diff --git a/src/Testcontainers/Clients/DockerImageOperations.cs b/src/Testcontainers/Clients/DockerImageOperations.cs index f469bd569..7dc2df009 100644 --- a/src/Testcontainers/Clients/DockerImageOperations.cs +++ b/src/Testcontainers/Clients/DockerImageOperations.cs @@ -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) @@ -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 @@ -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) @@ -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) diff --git a/src/Testcontainers/Clients/DockerNetworkOperations.cs b/src/Testcontainers/Clients/DockerNetworkOperations.cs index 503db9c0e..b0afbf444 100644 --- a/src/Testcontainers/Clients/DockerNetworkOperations.cs +++ b/src/Testcontainers/Clients/DockerNetworkOperations.cs @@ -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) @@ -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 diff --git a/src/Testcontainers/Clients/DockerSystemOperations.cs b/src/Testcontainers/Clients/DockerSystemOperations.cs index ad2751c85..72853fcfc 100644 --- a/src/Testcontainers/Clients/DockerSystemOperations.cs +++ b/src/Testcontainers/Clients/DockerSystemOperations.cs @@ -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; } diff --git a/src/Testcontainers/Clients/DockerVolumeOperations.cs b/src/Testcontainers/Clients/DockerVolumeOperations.cs index 4224bd92b..e1266d809 100644 --- a/src/Testcontainers/Clients/DockerVolumeOperations.cs +++ b/src/Testcontainers/Clients/DockerVolumeOperations.cs @@ -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) @@ -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 diff --git a/src/Testcontainers/Clients/IHasListOperations.cs b/src/Testcontainers/Clients/IHasListOperations.cs index e22229e6b..e5421df1d 100644 --- a/src/Testcontainers/Clients/IHasListOperations.cs +++ b/src/Testcontainers/Clients/IHasListOperations.cs @@ -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); } } diff --git a/src/Testcontainers/Clients/TestcontainersClient.cs b/src/Testcontainers/Clients/TestcontainersClient.cs index 2d9e45587..737b82bf3 100644 --- a/src/Testcontainers/Clients/TestcontainersClient.cs +++ b/src/Testcontainers/Clients/TestcontainersClient.cs @@ -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)) @@ -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)) diff --git a/src/Testcontainers/Images/FutureDockerImage.cs b/src/Testcontainers/Images/FutureDockerImage.cs index bb3b2be13..211c690b2 100644 --- a/src/Testcontainers/Images/FutureDockerImage.cs +++ b/src/Testcontainers/Images/FutureDockerImage.cs @@ -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); } diff --git a/src/Testcontainers/Images/MatchImage.cs b/src/Testcontainers/Images/MatchImage.cs index 8819cdcc7..58c5d227f 100644 --- a/src/Testcontainers/Images/MatchImage.cs +++ b/src/Testcontainers/Images/MatchImage.cs @@ -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)); } } } diff --git a/tests/Testcontainers.Tests/Unit/Configurations/ResourcePropertiesTest.cs b/tests/Testcontainers.Tests/Unit/Configurations/ResourcePropertiesTest.cs index 2e39f06ef..6b9c3d6b8 100644 --- a/tests/Testcontainers.Tests/Unit/Configurations/ResourcePropertiesTest.cs +++ b/tests/Testcontainers.Tests/Unit/Configurations/ResourcePropertiesTest.cs @@ -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() { diff --git a/tests/Testcontainers.Tests/Unit/Networks/TestcontainerNetworkBuilderTest.cs b/tests/Testcontainers.Tests/Unit/Networks/TestcontainerNetworkBuilderTest.cs index 3de38aa54..62c5bafc0 100644 --- a/tests/Testcontainers.Tests/Unit/Networks/TestcontainerNetworkBuilderTest.cs +++ b/tests/Testcontainers.Tests/Unit/Networks/TestcontainerNetworkBuilderTest.cs @@ -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 @@ -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 diff --git a/tests/Testcontainers.Tests/Unit/Volumes/TestcontainersVolumeBuilderTest.cs b/tests/Testcontainers.Tests/Unit/Volumes/TestcontainersVolumeBuilderTest.cs index 0b19a8101..ffccc525f 100644 --- a/tests/Testcontainers.Tests/Unit/Volumes/TestcontainersVolumeBuilderTest.cs +++ b/tests/Testcontainers.Tests/Unit/Volumes/TestcontainersVolumeBuilderTest.cs @@ -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