forked from testcontainers/testcontainers-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[testcontainers#466] #IMPLEMENT 'assemblyName: DotNet.Testcontainers;…
… function: Image Name Substitution'
- Loading branch information
Showing
12 changed files
with
250 additions
and
1 deletion.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/DotNet.Testcontainers/Builders/IImageNameSubstitutor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/DotNet.Testcontainers/Builders/ImageNameSubstitutor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 (!string.IsNullOrWhiteSpace(TestcontainersSettings.DockerHubImagePrefix)) | ||
{ | ||
return new PrefixingImageNameSubstitutor(TestcontainersSettings.DockerHubImagePrefix); | ||
} | ||
|
||
return new NoopImageSubstitutor(); | ||
} | ||
|
||
public class NoopImageSubstitutor : IImageNameSubstitutor | ||
{ | ||
public IDockerImage ApplyTo(IDockerImage originalImage) | ||
{ | ||
return originalImage; | ||
} | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/DotNet.Testcontainers/Builders/PrefixingImageNameSubstitutor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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() | ||
.NotWhitespace(); | ||
|
||
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
tests/DotNet.Testcontainers.Tests/Unit/Builders/ImageNameSubstitutorTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
tests/DotNet.Testcontainers.Tests/Unit/Builders/PrefixingImageNameSubstitutorTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
tests/DotNet.Testcontainers.Tests/Unit/Builders/TestcontainerBuilderTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters