From 2ba02ef80ad906a4c0f87084b0c5e44791599855 Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Thu, 5 Oct 2023 17:55:18 +0200 Subject: [PATCH] feat: Ignore FROM args while pre pulling images --- src/Testcontainers/Images/DockerfileArchive.cs | 8 +++----- .../Assets/pullBaseImages/Dockerfile | 1 + .../Unit/Images/ImageFromDockerfileTest.cs | 10 +++++++--- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Testcontainers/Images/DockerfileArchive.cs b/src/Testcontainers/Images/DockerfileArchive.cs index 4eda0f8bc..afac14a37 100644 --- a/src/Testcontainers/Images/DockerfileArchive.cs +++ b/src/Testcontainers/Images/DockerfileArchive.cs @@ -89,10 +89,6 @@ public IEnumerable GetBaseImages() .Where(line => !line.StartsWith("#", StringComparison.Ordinal)) .Select(line => FromLinePattern.Match(line)) .Where(match => match.Success) - // Until now, we are unable to resolve variables within Dockerfiles. Ignore base - // images that utilize variables. Expect them to exist on the host. - .Where(match => !match.Groups[imageGroup].Value.Contains('$')) - .Where(match => !match.Groups[imageGroup].Value.Any(char.IsUpper)) .ToArray(); var stages = lines @@ -105,7 +101,9 @@ public IEnumerable GetBaseImages() var images = lines .Select(match => match.Groups[imageGroup]) - .Select(group => group.Value) + .Select(match => match.Value) + .Where(line => !line.Contains('$')) + .Where(line => !line.Any(char.IsUpper)) .Where(value => !stages.Contains(value)) .Distinct() .Select(value => new DockerImage(value)) diff --git a/tests/Testcontainers.Tests/Assets/pullBaseImages/Dockerfile b/tests/Testcontainers.Tests/Assets/pullBaseImages/Dockerfile index 05d7b2c45..dcce54943 100644 --- a/tests/Testcontainers.Tests/Assets/pullBaseImages/Dockerfile +++ b/tests/Testcontainers.Tests/Assets/pullBaseImages/Dockerfile @@ -6,3 +6,4 @@ FROM mcr.microsoft.com/dotnet/runtime:6.0 AS runtime FROM build FROM build AS publish FROM mcr.microsoft.com/dotnet/aspnet:6.0.21-jammy-amd64 +FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/aspnet:6.0.22-jammy-amd64 diff --git a/tests/Testcontainers.Tests/Unit/Images/ImageFromDockerfileTest.cs b/tests/Testcontainers.Tests/Unit/Images/ImageFromDockerfileTest.cs index 3803cd16b..c44d20ea6 100644 --- a/tests/Testcontainers.Tests/Unit/Images/ImageFromDockerfileTest.cs +++ b/tests/Testcontainers.Tests/Unit/Images/ImageFromDockerfileTest.cs @@ -21,13 +21,17 @@ public void DockerfileArchiveGetBaseImages() // Given IImage image = new DockerImage("localhost/testcontainers", Guid.NewGuid().ToString("D"), string.Empty); - var dockerfileArchive = new DockerfileArchive("Assets//pullBaseImages/", "Dockerfile", image, NullLogger.Instance); + var dockerfileArchive = new DockerfileArchive("Assets/pullBaseImages/", "Dockerfile", image, NullLogger.Instance); // When - var baseImages = dockerfileArchive.GetBaseImages(); + var baseImages = dockerfileArchive.GetBaseImages().ToArray(); // Then - Assert.Equal(3, baseImages.Count()); + Assert.Equal(4, baseImages.Length); + Assert.Contains(baseImages, item => "mcr.microsoft.com/dotnet/sdk:6.0".Equals(item.FullName)); + Assert.Contains(baseImages, item => "mcr.microsoft.com/dotnet/runtime:6.0".Equals(item.FullName)); + Assert.Contains(baseImages, item => "mcr.microsoft.com/dotnet/aspnet:6.0.21-jammy-amd64".Equals(item.FullName)); + Assert.Contains(baseImages, item => "mcr.microsoft.com/dotnet/aspnet:6.0.22-jammy-amd64".Equals(item.FullName)); } [Fact]