Skip to content

Commit

Permalink
feat: Support digest in IImage, DockerImage and in the `WithImage…
Browse files Browse the repository at this point in the history
…(string)` implementation (#1249)

Co-authored-by: Andre Hofmeister <[email protected]>
  • Loading branch information
Kielek and HofmeisterAn authored Sep 15, 2024
1 parent 1d32bc8 commit 4834c3b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Testcontainers/Builders/DockerConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace DotNet.Testcontainers.Builders
namespace DotNet.Testcontainers.Builders
{
using System;
using System.IO;
Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers/Builders/SourceGenerationContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace DotNet.Testcontainers.Builders
namespace DotNet.Testcontainers.Builders
{
using System.Text.Json.Serialization;

Expand Down
3 changes: 1 addition & 2 deletions src/Testcontainers/Clients/DockerImageOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public async Task CreateAsync(IImage image, IDockerRegistryAuthenticationConfigu
{
var createParameters = new ImagesCreateParameters
{
FromImage = image.FullName.Replace(":" + image.Tag, string.Empty), // Workaround until https://github.com/dotnet/Docker.DotNet/issues/595 is fixed.
Tag = image.Tag,
FromImage = image.FullName
};

var authConfig = new AuthConfig
Expand Down
13 changes: 11 additions & 2 deletions src/Testcontainers/Images/DockerImage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace DotNet.Testcontainers.Images
namespace DotNet.Testcontainers.Images
{
using System;
using System.Globalization;
Expand Down Expand Up @@ -117,7 +117,16 @@ public DockerImage(
public string Digest => _digit;

/// <inheritdoc />
public string FullName => $"{Registry}/{Repository}:{Tag}".Trim(TrimChars);
public string FullName
{
get
{
var registry = string.IsNullOrEmpty(Registry) ? string.Empty : $"{Registry}/";
var tag = string.IsNullOrEmpty(Tag) ? string.Empty : $":{Tag}";
var digest = string.IsNullOrEmpty(Digest) ? string.Empty : $"@{Digest}";
return $"{registry}{Repository}{tag}{digest}";
}
}

/// <inheritdoc />
[Obsolete("We will remove this property, it does not follow the DSL. Use the 'Repository' property instead.")]

Check warning on line 132 in src/Testcontainers/Images/DockerImage.cs

View workflow job for this annotation

GitHub Actions / publish

Do not forget to remove this deprecated code someday. (https://rules.sonarsource.com/csharp/RSPEC-1133)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,23 @@ await container.StartAsync()
Assert.EndsWith(name, container.Name);
}

[Fact]
public async Task PullDigest()
{
// Given
await using var container = new ContainerBuilder()
.WithImage("alpine@sha256:3451da08fc6ef554a100da3e2df5ac6d598c82f2a774d5f6ed465c3d80cd163a")
.WithEntrypoint(CommonCommands.SleepInfinity)
.Build();

// When
var exception = await Record.ExceptionAsync(() => container.StartAsync())
.ConfigureAwait(true);

// Then
Assert.Null(exception);
}

[Fact]
public async Task PullPolicyNever()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void ShouldNotThrowArgumentExceptionIfImageTagHasUppercaseCharacters(stri

[Theory]
[ClassData(typeof(DockerImageFixture))]
public void WhenImageNameGetsAssigned(DockerImageFixtureSerializable serializable, string image, string _)
public void WhenImageNameGetsAssigned(DockerImageFixtureSerializable serializable, string image, string fullName)
{
// Given
var expected = serializable.Image;
Expand All @@ -67,7 +67,7 @@ public void WhenImageNameGetsAssigned(DockerImageFixtureSerializable serializabl
Assert.Equal(expected.Digest, actual.Digest);
Assert.Equal(expected.FullName, actual.FullName);
Assert.Equal(expected.Name, actual.Name);
// Assert.Equal(fullName, actual.FullName);
Assert.Equal(fullName, actual.FullName);
}

[Fact]
Expand Down

0 comments on commit 4834c3b

Please sign in to comment.