From f3096c089008a435fccd36536c3a33c7f8e62026 Mon Sep 17 00:00:00 2001 From: Christopher Bohlen Date: Wed, 8 Jun 2022 08:19:01 +0200 Subject: [PATCH] [#470] #BUGFIX 'assemblyName: DotNet.Testcontainers; function: DockerImage' {Consider hostname of Docker Hub image name prefix when configured.} --- src/DotNet.Testcontainers/Images/DockerImage.cs | 2 +- .../DockerRegistryAuthenticationProviderTest.cs | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/DotNet.Testcontainers/Images/DockerImage.cs b/src/DotNet.Testcontainers/Images/DockerImage.cs index e5f3231cd..c6f0f1eb8 100644 --- a/src/DotNet.Testcontainers/Images/DockerImage.cs +++ b/src/DotNet.Testcontainers/Images/DockerImage.cs @@ -91,7 +91,7 @@ public string FullName /// public string GetHostname() { - var firstSegmentOfRepository = this.Repository.Split('/').First(); + var firstSegmentOfRepository = (string.IsNullOrEmpty(this.hubImageNamePrefix) ? this.Repository : this.hubImageNamePrefix).Split('/').First(); return firstSegmentOfRepository.IndexOfAny(new[] { '.', ':' }) >= 0 ? firstSegmentOfRepository : null; } } diff --git a/tests/DotNet.Testcontainers.Tests/Unit/Configurations/DockerRegistryAuthenticationProviderTest.cs b/tests/DotNet.Testcontainers.Tests/Unit/Configurations/DockerRegistryAuthenticationProviderTest.cs index 33ccb0486..ffc7554de 100644 --- a/tests/DotNet.Testcontainers.Tests/Unit/Configurations/DockerRegistryAuthenticationProviderTest.cs +++ b/tests/DotNet.Testcontainers.Tests/Unit/Configurations/DockerRegistryAuthenticationProviderTest.cs @@ -37,6 +37,17 @@ public void GetHostnameFromDockerImage(string dockerImageName, string hostname) Assert.Equal(hostname, image.GetHostname()); } + [Theory] + [InlineData("", "docker", "stable")] + [InlineData("fedora", "httpd", "1.0")] + [InlineData("foo/bar", "baz", "1.0.0")] + public void GetHostnameFromHubImageNamePrefix(string repository, string name, string tag) + { + const string hubImageNamePrefix = "myregistry.azurecr.io"; + IDockerImage image = new DockerImage(repository, name, tag, hubImageNamePrefix); + Assert.Equal(hubImageNamePrefix, image.GetHostname()); + } + [Fact] public void ShouldGetDefaultDockerRegistryAuthenticationConfiguration() {