Skip to content

Commit

Permalink
[testcontainers#466] #IMPLEMENT 'assemblyName: DotNet.Testcontainers;…
Browse files Browse the repository at this point in the history
… function: Image Name Substitution'
  • Loading branch information
bohlenc committed Jun 2, 2022
1 parent ba7d87b commit 06cfe41
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/DotNet.Testcontainers/Builders/TestcontainersBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public ITestcontainersBuilder<TDockerContainer> WithImage(string image)
/// <inheritdoc cref="ITestcontainersBuilder{TDockerContainer}" />
public ITestcontainersBuilder<TDockerContainer> WithImage(IDockerImage image)
{
return this.MergeNewConfiguration(new TestcontainersConfiguration(image: image));
var modifiedImage = MaybePrependPrefixToImageName(image);
return this.MergeNewConfiguration(new TestcontainersConfiguration(image: modifiedImage));
}

/// <inheritdoc cref="ITestcontainersBuilder{TDockerContainer}" />
Expand Down Expand Up @@ -330,5 +331,22 @@ protected virtual ITestcontainersBuilder<TDockerContainer> MergeNewConfiguration
var updatedDockerResourceConfiguration = new TestcontainersConfiguration(endpoint, dockerRegistryAuthConfig, image, name, hostname, workingDirectory, entrypoint, command, environments, labels, exposedPorts, portBindings, mounts, networks, outputConsumer, waitStrategies, startupCallback, autoRemove, privileged);
return new TestcontainersBuilder<TDockerContainer>(updatedDockerResourceConfiguration, moduleConfiguration ?? this.mergeModuleConfiguration);
}

private static IDockerImage MaybePrependPrefixToImageName(IDockerImage originalImage)
{
var shouldPrependPrefix = !string.IsNullOrWhiteSpace(TestcontainersSettings.DockerHubImagePrefix);
if (!shouldPrependPrefix)
{
return originalImage;
}

var hostedOnDockerHub = string.IsNullOrWhiteSpace(originalImage.GetHostname());
if (!hostedOnDockerHub)
{
return originalImage;
}

return new DockerImage(originalImage.Repository.Length == 0 ? TestcontainersSettings.DockerHubImagePrefix : $"{TestcontainersSettings.DockerHubImagePrefix}/{originalImage.Repository}", originalImage.Name, originalImage.Tag);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public static class TestcontainersSettings
public static bool ResourceReaperEnabled { get; set; }
= true;

/// <summary>
/// Gets or sets a prefix to prepend to image names for images hosted on DockerHub.
/// </summary>
public static string DockerHubImagePrefix { get; set; }

/// <summary>
/// Gets or sets the logger.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/DotNet.Testcontainers/Containers/IDockerContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ public interface IRunningDockerContainer
[NotNull]
string Hostname { get; }

/// <summary>
/// Gets the Testcontainers image name.
/// </summary>
/// <value>
/// Returns the name of the image this container has been created from.
/// </value>
[NotNull]
string ImageName { get; }

/// <summary>
/// Gets the Testcontainers state.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ public string Hostname
}
}

/// <inheritdoc />
public string ImageName
{
get
{
return this.configuration.Image.FullName;
}
}

/// <inheritdoc />
public TestcontainersState State
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
namespace DotNet.Testcontainers.Tests.Unit.Builders;

using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Containers;
using DotNet.Testcontainers.Images;
using Xunit;

[Collection("Modifies TestcontainersSettings.DockerHubImagePrefix")]
public class TestcontainerBuilderTest
{
[Theory]
[InlineData("my.proxy.com", "bar", "my.proxy.com/bar:latest")]
[InlineData("my.proxy.com", "bar:latest", "my.proxy.com/bar:latest")]
[InlineData("my.proxy.com", "bar:1.0.0", "my.proxy.com/bar:1.0.0")]
[InlineData("my.proxy.com/my-path", "bar:1.0.0", "my.proxy.com/my-path/bar:1.0.0")]
[InlineData("my.proxy.com:443", "bar:1.0.0", "my.proxy.com:443/bar:1.0.0")]
[InlineData("my.proxy.com", "foo/bar:1.0.0", "my.proxy.com/foo/bar:1.0.0")]
[InlineData("my.proxy.com/my-path", "foo/bar:1.0.0", "my.proxy.com/my-path/foo/bar:1.0.0")]
[InlineData("my.proxy.com:443", "foo/bar:1.0.0", "my.proxy.com:443/foo/bar:1.0.0")]
[InlineData("my.proxy.com:443/my-path", "foo/bar:1.0.0", "my.proxy.com:443/my-path/foo/bar:1.0.0")]
[InlineData("my.proxy.com", "myregistry.azurecr.io/foo/bar:1.0.0", "myregistry.azurecr.io/foo/bar:1.0.0")]
[InlineData("my.proxy.com", "myregistry.azurecr.io:443/foo/bar:1.0.0", "myregistry.azurecr.io:443/foo/bar:1.0.0")]
public void ImageNameFromStringIsPrefixedWhenPrefixIsSet(string prefix, string originalImageName, string expectedName)
{
TestcontainersSettings.DockerHubImagePrefix = prefix;

var container = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage(originalImageName)
.Build();

Assert.Equal(expectedName, container.ImageName);
}

[Theory]
[InlineData("my.proxy.com", "bar", "my.proxy.com/bar:latest")]
[InlineData("my.proxy.com", "bar:latest", "my.proxy.com/bar:latest")]
[InlineData("my.proxy.com", "bar:1.0.0", "my.proxy.com/bar:1.0.0")]
[InlineData("my.proxy.com/my-path", "bar:1.0.0", "my.proxy.com/my-path/bar:1.0.0")]
[InlineData("my.proxy.com:443", "bar:1.0.0", "my.proxy.com:443/bar:1.0.0")]
[InlineData("my.proxy.com", "foo/bar:1.0.0", "my.proxy.com/foo/bar:1.0.0")]
[InlineData("my.proxy.com/my-path", "foo/bar:1.0.0", "my.proxy.com/my-path/foo/bar:1.0.0")]
[InlineData("my.proxy.com:443", "foo/bar:1.0.0", "my.proxy.com:443/foo/bar:1.0.0")]
[InlineData("my.proxy.com:443/my-path", "foo/bar:1.0.0", "my.proxy.com:443/my-path/foo/bar:1.0.0")]
[InlineData("my.proxy.com", "myregistry.azurecr.io/foo/bar:1.0.0", "myregistry.azurecr.io/foo/bar:1.0.0")]
[InlineData("my.proxy.com", "myregistry.azurecr.io:443/foo/bar:1.0.0", "myregistry.azurecr.io:443/foo/bar:1.0.0")]
public void ImageNameFromClassIsPrefixedWhenPrefixIsSet(string prefix, string originalImageName, string expectedName)
{
TestcontainersSettings.DockerHubImagePrefix = prefix;

var container = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage(new DockerImage(originalImageName))
.Build();

Assert.Equal(expectedName, container.ImageName);
}

[Fact]
public void ImageNameIsNotPrefixedWhenPrefixIsNotSet()
{
TestcontainersSettings.DockerHubImagePrefix = null;

var container = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("foo/bar:1.0.0")
.Build();

Assert.Equal("foo/bar:1.0.0", container.ImageName);
}
}

0 comments on commit 06cfe41

Please sign in to comment.