From 2d3570859f22e419d641484caf27b6c857ff855f Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Mon, 4 Nov 2024 17:23:28 +0100 Subject: [PATCH] fix: Prepend Docker Hub namespace prefix to repository --- src/Testcontainers/Images/DockerImage.cs | 54 ++++++++++++++----- .../Fixtures/Images/DockerImageFixture.cs | 4 ++ 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/Testcontainers/Images/DockerImage.cs b/src/Testcontainers/Images/DockerImage.cs index 43be861dd..d41a4c1d7 100644 --- a/src/Testcontainers/Images/DockerImage.cs +++ b/src/Testcontainers/Images/DockerImage.cs @@ -15,6 +15,8 @@ public sealed class DockerImage : IImage private static readonly char[] TrimChars = [' ', ':', '/']; + private static readonly char[] SlashChar = ['/']; + private static readonly Func GetDockerImage = MatchImage.Match; [NotNull] @@ -27,10 +29,7 @@ public sealed class DockerImage : IImage private readonly string _tag; [CanBeNull] - private readonly string _digit; - - [CanBeNull] - private readonly string _hubImageNamePrefix; + private readonly string _digest; /// /// Initializes a new instance of the class. @@ -66,32 +65,63 @@ public DockerImage( string tag = null, string digest = null, string hubImageNamePrefix = null) + : this( + TrimOrDefault(repository), + TrimOrDefault(registry), + TrimOrDefault(tag, tag == null && digest == null ? LatestTag : null), + TrimOrDefault(digest), + hubImageNamePrefix == null ? [] : hubImageNamePrefix.Trim(TrimChars).Split(SlashChar, 2, StringSplitOptions.RemoveEmptyEntries)) + { + } + + private DockerImage( + string repository, + string registry, + string tag, + string digest, + string[] substitutions) { _ = Guard.Argument(repository, nameof(repository)) .NotNull() .NotEmpty() .NotUppercase(); - var defaultTag = tag == null && digest == null ? LatestTag : null; + _ = Guard.Argument(substitutions, nameof(substitutions)) + .NotNull(); + + // The Docker Hub image name prefix may include namespaces, which we need to extract + // and prepend to the repository name. The registry itself contains only the hostname. + switch (substitutions.Length) + { + case 2: + _repository = string.Join("/", substitutions[1], repository); + _registry = substitutions[0]; + break; + case 1: + _repository = repository; + _registry = substitutions[0]; + break; + default: + _repository = repository; + _registry = registry; + break; + } - _repository = TrimOrDefault(repository); - _registry = TrimOrDefault(registry); - _tag = TrimOrDefault(tag, defaultTag); - _digit = TrimOrDefault(digest); - _hubImageNamePrefix = TrimOrDefault(hubImageNamePrefix); + _tag = tag; + _digest = digest; } /// public string Repository => _repository; /// - public string Registry => string.IsNullOrEmpty(_hubImageNamePrefix) ? _registry : _hubImageNamePrefix; + public string Registry => _registry; /// public string Tag => _tag; /// - public string Digest => _digit; + public string Digest => _digest; /// public string FullName diff --git a/tests/Testcontainers.Tests/Fixtures/Images/DockerImageFixture.cs b/tests/Testcontainers.Tests/Fixtures/Images/DockerImageFixture.cs index 80ce04da5..69e7ee419 100644 --- a/tests/Testcontainers.Tests/Fixtures/Images/DockerImageFixture.cs +++ b/tests/Testcontainers.Tests/Fixtures/Images/DockerImageFixture.cs @@ -16,6 +16,8 @@ public sealed class DockerImageFixture : TheoryData