-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
… function: Image Name Substitution'
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace DotNet.Testcontainers.Builders | ||
{ | ||
using DotNet.Testcontainers.Images; | ||
|
||
/// <summary> | ||
/// Substitutes the fullname of a <see cref="IDockerImage"/>. | ||
/// The nature of the substitution is up to the implementation. | ||
/// </summary> | ||
public interface IImageNameSubstitutor | ||
{ | ||
/// <summary> | ||
/// Applies a substitution to the fullname of an image. | ||
/// </summary> | ||
/// <param name="originalImage"> | ||
/// The image to modify. | ||
/// </param> | ||
/// <returns> | ||
/// The modified image. | ||
/// </returns> | ||
IDockerImage ApplyTo(IDockerImage originalImage); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace DotNet.Testcontainers.Builders | ||
{ | ||
using DotNet.Testcontainers.Configurations; | ||
using DotNet.Testcontainers.Images; | ||
|
||
public class ImageNameSubstitutor | ||
{ | ||
public static IImageNameSubstitutor Create() | ||
{ | ||
if (TestcontainersSettings.DockerHubImagePrefix != null) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
{ | ||
return new PrefixingImageNameSubstitutor(TestcontainersSettings.DockerHubImagePrefix); | ||
} | ||
|
||
return new NoopImageSubstitutor(); | ||
} | ||
|
||
public class NoopImageSubstitutor : IImageNameSubstitutor | ||
{ | ||
public IDockerImage ApplyTo(IDockerImage originalImage) | ||
{ | ||
return originalImage; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
namespace DotNet.Testcontainers.Builders | ||
{ | ||
using DotNet.Testcontainers.Images; | ||
|
||
/// <summary> | ||
/// Prepends a prefix to the fullname of a Docker image. | ||
/// Applies only to images hosted on Docker Hub. | ||
/// </summary> | ||
/// <example> | ||
/// With a configured prefix of "my.proxy.com": | ||
/// docker:latest -> my.proxy.com/docker:latest | ||
/// my.azurecr.io/docker:latest -> my.azurecr.io/docker:latest | ||
/// </example> | ||
public class PrefixingImageNameSubstitutor : IImageNameSubstitutor | ||
{ | ||
/// <summary> | ||
/// The prefix to prepend to the fullname of the image. | ||
/// </summary> | ||
private readonly string prefix; | ||
|
||
public PrefixingImageNameSubstitutor(string prefix) | ||
{ | ||
Guard.Argument(prefix, nameof(prefix)) | ||
.NotNull(); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
bohlenc
Author
Owner
|
||
|
||
this.prefix = prefix; | ||
} | ||
|
||
/// <inheritdoc />> | ||
public IDockerImage ApplyTo(IDockerImage originalImage) | ||
{ | ||
var imageHostedOnDockerHub = originalImage.GetHostname() == null; | ||
if (!imageHostedOnDockerHub) | ||
{ | ||
return originalImage; | ||
} | ||
|
||
return new DockerImage(originalImage.Repository.Length == 0 ? this.prefix : $"{this.prefix}/{originalImage.Repository}", originalImage.Name, originalImage.Tag); | ||
This comment has been minimized.
Sorry, something went wrong.
PSanetra
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ namespace DotNet.Testcontainers.Configurations | |
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Builders; | ||
using DotNet.Testcontainers.Containers; | ||
using DotNet.Testcontainers.Images; | ||
using DotNet.Testcontainers.Networks; | ||
|
@@ -100,5 +101,16 @@ public interface ITestcontainersConfiguration : IDockerResourceConfiguration | |
/// This callback will be executed after starting the container, but before executing the wait strategies. | ||
/// </remarks> | ||
Func<ITestcontainersContainer, CancellationToken, Task> StartupCallback { get; } | ||
|
||
/// <summary> | ||
/// Applies the given <see cref="IImageNameSubstitutor" /> to the configured <see cref="IDockerImage" />. | ||
/// </summary> | ||
/// <param name="imageNameSubstitutor"> | ||
/// The <see cref="IImageNameSubstitutor" /> to apply | ||
/// </param> | ||
/// <returns> | ||
/// Configuration with the modified <see cref="IDockerImage" /> | ||
/// </returns> | ||
ITestcontainersConfiguration Apply(IImageNameSubstitutor imageNameSubstitutor); | ||
This comment has been minimized.
Sorry, something went wrong.
PSanetra
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace DotNet.Testcontainers.Tests.Unit.Builders; | ||
|
||
using DotNet.Testcontainers.Builders; | ||
using DotNet.Testcontainers.Configurations; | ||
using Xunit; | ||
|
||
public class ImageNameSubstitutorTest | ||
{ | ||
[Fact] | ||
public void CreatesNoopImageNameSubstitutorWhenNothingElseIsConfigured() | ||
{ | ||
var substitutor = ImageNameSubstitutor.Create(); | ||
|
||
Assert.IsType<ImageNameSubstitutor.NoopImageSubstitutor>(substitutor); | ||
} | ||
|
||
[Fact] | ||
public void CreatesPrefixingImageNameSubstitutorWhenDockerHubImagePrefixIsConfigured() | ||
{ | ||
TestcontainersSettings.DockerHubImagePrefix = "my.proxy.com"; | ||
|
||
var substitutor = ImageNameSubstitutor.Create(); | ||
|
||
Assert.IsType<PrefixingImageNameSubstitutor>(substitutor); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
namespace DotNet.Testcontainers.Tests.Unit.Builders; | ||
|
||
using DotNet.Testcontainers.Builders; | ||
using DotNet.Testcontainers.Images; | ||
using Xunit; | ||
|
||
public class PrefixingImageNameSubstitutorTest | ||
{ | ||
|
||
[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", "foo/bar:1.0.0", "my.proxy.com/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", "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 ShouldAddPrefixForDockerHubImages(string prefix, string originalImageFullName, string expectedFullName) | ||
{ | ||
var substitutor = new PrefixingImageNameSubstitutor(prefix); | ||
|
||
var originalImage = new DockerImage(originalImageFullName); | ||
|
||
var actual = substitutor.ApplyTo(originalImage); | ||
|
||
Assert.Equal(expectedFullName, actual.FullName); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace DotNet.Testcontainers.Tests.Unit.Builders; | ||
|
||
using DotNet.Testcontainers.Builders; | ||
using DotNet.Testcontainers.Configurations; | ||
using DotNet.Testcontainers.Containers; | ||
using Xunit; | ||
|
||
public class TestcontainerBuilderTest | ||
{ | ||
[Fact] | ||
public void BuildAppliesDoesNotChangeImageNameWhenNoSubstitutorIsConfigured() | ||
{ | ||
TestcontainersSettings.DockerHubImagePrefix = null; | ||
|
||
var container = new TestcontainersBuilder<TestcontainersContainer>() | ||
.WithImage("foo/bar:1.0.0") | ||
.Build(); | ||
|
||
Assert.Equal("foo/bar:1.0.0", container.ImageName); | ||
} | ||
|
||
[Fact] | ||
public void BuildAppliesPrexifingImageNameSubstitutorWhenDockerHubImagePrefixIsConfigured() | ||
{ | ||
TestcontainersSettings.DockerHubImagePrefix = "my.proxy.com"; | ||
|
||
var container = new TestcontainersBuilder<TestcontainersContainer>() | ||
.WithImage("foo/bar:1.0.0") | ||
.Build(); | ||
|
||
Assert.Equal("my.proxy.com/foo/bar:1.0.0", container.ImageName); | ||
} | ||
} |
I think I would replace this with !string.IsNullOrEmpty()