File: Publishing\PodmanSaveArgumentsTests.cs
Web Access
Project: src\tests\Aspire.Hosting.Tests\Aspire.Hosting.Tests.csproj (Aspire.Hosting.Tests)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
#pragma warning disable ASPIRECONTAINERRUNTIME001
#pragma warning disable ASPIREPIPELINES003
 
using System.Text.RegularExpressions;
using Aspire.Hosting.Dcp.Process;
using Aspire.Hosting.Publishing;
using Aspire.Hosting.Tests.Utils;
using Aspire.Hosting.Utils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
 
namespace Aspire.Hosting.Tests.Publishing;
 
public class PodmanSaveArgumentsTests(ITestOutputHelper output)
{
    [Fact]
    public void BuildSaveArguments_OciFormat_UsesOciArchive()
    {
        var options = new ContainerImageBuildOptions
        {
            ImageName = "myapp",
            Tag = "latest",
            OutputPath = Path.Combine("out", "archives"),
            ImageFormat = ContainerImageFormat.Oci
        };
 
        var arguments = PodmanContainerRuntime.BuildSaveArguments("myapp:latest", options);
 
        var expectedPath = Path.Combine("out", "archives", "myapp-latest.tar");
        Assert.Equal($"save --format \"oci-archive\" --output \"{expectedPath}\" \"myapp:latest\"", arguments);
    }
 
    [Fact]
    public void BuildSaveArguments_DockerFormat_UsesDockerArchive()
    {
        var options = new ContainerImageBuildOptions
        {
            ImageName = "myapp",
            Tag = "latest",
            OutputPath = Path.Combine("out", "archives"),
            ImageFormat = ContainerImageFormat.Docker
        };
 
        var arguments = PodmanContainerRuntime.BuildSaveArguments("myapp:latest", options);
 
        var expectedPath = Path.Combine("out", "archives", "myapp-latest.tar");
        Assert.Equal($"save --format \"docker-archive\" --output \"{expectedPath}\" \"myapp:latest\"", arguments);
    }
 
    [Fact]
    public void BuildSaveArguments_UnspecifiedFormat_DefaultsToDockerArchive()
    {
        var options = new ContainerImageBuildOptions
        {
            ImageName = "myapp",
            Tag = "latest",
            OutputPath = Path.Combine("out", "archives")
        };
 
        var arguments = PodmanContainerRuntime.BuildSaveArguments("myapp:latest", options);
 
        var expectedPath = Path.Combine("out", "archives", "myapp-latest.tar");
        Assert.Equal($"save --format \"docker-archive\" --output \"{expectedPath}\" \"myapp:latest\"", arguments);
    }
 
    [Fact]
    public void BuildSaveArguments_ArchivePathMatchesDockerRuntime()
    {
        // Both runtimes must write the same archive file for the same resource, otherwise consumers that
        // resolve the archive path cannot find what Podman produced.
        var outputPath = Path.Combine("out", "archives");
        var options = new ContainerImageBuildOptions
        {
            ImageName = "registry.example.com/team/myapp",
            Tag = "1.2.3",
            OutputPath = outputPath,
            ImageFormat = ContainerImageFormat.Oci
        };
 
        var arguments = PodmanContainerRuntime.BuildSaveArguments("registry.example.com/team/myapp:1.2.3", options);
 
        var dockerArchivePath = ResourceExtensions.GetContainerImageArchivePath(outputPath, "registry.example.com/team/myapp:1.2.3");
        Assert.Contains($"--output \"{dockerArchivePath}\"", arguments, StringComparison.Ordinal);
    }
 
    [Fact]
    public async Task BuildSaveArguments_ArchivePathMatchesContainerImageReference()
    {
        // The runtimes write the archive, but ContainerImageReference is what tells consumers where to
        // find it, so the producer and the consumer have to resolve the same file. A registry-qualified
        // image name is the case where they used to diverge: the runtimes flattened the repository
        // segments while ContainerImageReference left the '/' in place, pointing consumers at a nested
        // path nothing ever wrote.
        var outputPath = Path.Combine("out", "archives");
        const string ImageName = "registry.example.com/team/myapp";
        const string ImageTag = "1.2.3";
 
        using var builder = TestDistributedApplicationBuilder.Create(DistributedApplicationOperation.Publish);
 
        var container = builder.AddContainer("myapp", ImageName)
            .WithContainerBuildOptions(ctx =>
            {
                ctx.Destination = ContainerImageDestination.Archive;
                ctx.ImageFormat = ContainerImageFormat.Oci;
                ctx.OutputPath = outputPath;
                ctx.LocalImageName = ImageName;
                ctx.LocalImageTag = ImageTag;
            });
 
        using var app = builder.Build();
 
        IValueProvider imageReference = new ContainerImageReference(container.Resource);
        var consumerPath = await imageReference.GetValueAsync(
            new ValueProviderContext { ExecutionContext = app.Services.GetRequiredService<DistributedApplicationExecutionContext>() },
            CancellationToken.None);
 
        var options = new ContainerImageBuildOptions
        {
            ImageName = ImageName,
            Tag = ImageTag,
            OutputPath = outputPath,
            ImageFormat = ContainerImageFormat.Oci
        };
 
        var arguments = PodmanContainerRuntime.BuildSaveArguments($"{ImageName}:{ImageTag}", options);
 
        Assert.Equal(
            $"save --format \"oci-archive\" --output \"{consumerPath}\" \"{ImageName}:{ImageTag}\"",
            arguments);
    }
 
    [Fact]
    public void BuildSaveArguments_FileOutputPath_SavesToThatFile()
    {
        var archivePath = Path.Combine("Images", "worker.tar");
        var options = new ContainerImageBuildOptions
        {
            ImageName = "worker",
            Tag = "latest",
            OutputPath = archivePath,
            ImageFormat = ContainerImageFormat.Oci
        };
 
        var arguments = PodmanContainerRuntime.BuildSaveArguments("worker:latest", options);
 
        Assert.Equal($"save --format \"oci-archive\" --output \"{archivePath}\" \"worker:latest\"", arguments);
    }
 
    [Fact]
    public void BuildSaveArguments_TagIsPreservedInArchiveName()
    {
        // Dropping the tag would place the archive at a path nothing else resolves to.
        var options = new ContainerImageBuildOptions
        {
            ImageName = "container",
            Tag = "abc123",
            OutputPath = "out",
            ImageFormat = ContainerImageFormat.Oci
        };
 
        var arguments = PodmanContainerRuntime.BuildSaveArguments("container:abc123", options);
 
        Assert.Contains($"--output \"{Path.Combine("out", "container-abc123.tar")}\"", arguments, StringComparison.Ordinal);
    }
 
    [Theory]
    [InlineData(null)]
    [InlineData("")]
    public async Task BuildImageAsync_OciFormatWithoutOutputPath_ThrowsBeforeBuild(string? outputPath)
    {
        var processRunner = new TestProcessRunner();
        var runtime = CreateRuntime(processRunner);
        var options = new ContainerImageBuildOptions
        {
            ImageName = "myapp",
            Tag = "latest",
            OutputPath = outputPath,
            ImageFormat = ContainerImageFormat.Oci
        };
 
        var exception = await Assert.ThrowsAsync<ArgumentException>(() => BuildImageAsync(runtime, options));
 
        Assert.Equal("options", exception.ParamName);
        Assert.Empty(processRunner.ProcessSpecs);
    }
 
    [Theory]
    [InlineData(null)]
    [InlineData("")]
    public async Task BuildImageAsync_WithoutOutputPath_DoesNotSave(string? outputPath)
    {
        var processRunner = new TestProcessRunner();
        processRunner.EnqueueResult();
        var runtime = CreateRuntime(processRunner);
        var options = new ContainerImageBuildOptions
        {
            ImageName = "myapp",
            Tag = "latest",
            OutputPath = outputPath,
            ImageFormat = ContainerImageFormat.Docker
        };
 
        await BuildImageAsync(runtime, options);
 
        var buildSpec = Assert.Single(processRunner.ProcessSpecs);
        Assert.Equal("build", buildSpec.ArgumentList![0]);
    }
 
    [Theory]
    [InlineData("myapp")]
    // A dotted single-component name is still unqualified, so Podman saves it as localhost/worker.api.
    [InlineData("worker.api")]
    public async Task BuildImageAsync_WithOutputPath_BuildsUnderPrivateTagAndWritesRequestedTag(string imageName)
    {
        using var workspace = TemporaryWorkspace.Create(output);
        var outputPath = workspace.WorkspaceRoot.FullName;
        var processRunner = CreateSaveProcessRunner();
        var runtime = CreateRuntime(processRunner);
        var options = new ContainerImageBuildOptions
        {
            ImageName = imageName,
            Tag = "latest",
            OutputPath = outputPath,
            ImageFormat = ContainerImageFormat.Docker
        };
 
        await BuildImageAsync(runtime, options);
 
        // `podman build` stores the image locally, so the requested <name>:latest must never be tagged there.
        // The build and save use a private tag, which is removed afterwards.
        var archivePath = ResourceExtensions.GetContainerImageArchivePath(outputPath, $"{imageName}:latest");
        string? privateImage = null;
        Assert.Collection(
            processRunner.ProcessSpecs,
            build =>
            {
                Assert.Equal("build", build.ArgumentList![0]);
                privateImage = GetBuildTag(build);
                Assert.Matches($"^{Regex.Escape(imageName)}:aspire-archive-[0-9a-f]{{32}}$", privateImage);
            },
            save =>
            {
                // The private image is saved to a staging file beside the archive, never to the archive path.
                var match = Regex.Match(Assert.IsType<string>(save.Arguments), "^save --format \"docker-archive\" --output \"(?<path>[^\"]+)\" \"(?<image>[^\"]+)\"$");
                Assert.True(match.Success, save.Arguments);
                Assert.Equal(privateImage, match.Groups["image"].Value);
                Assert.Equal(Path.GetDirectoryName(archivePath), Path.GetDirectoryName(match.Groups["path"].Value));
                Assert.Matches($"^\\.{Regex.Escape(Path.GetFileName(archivePath))}\\.[0-9a-f]{{32}}\\.save\\.tmp$", Path.GetFileName(match.Groups["path"].Value));
            },
            remove => Assert.Equal($"rmi \"{privateImage}\"", remove.Arguments));
 
        Assert.Equal([$"localhost/{imageName}:latest"], TestContainerImageArchive.ReadDockerImageReferences(archivePath));
        Assert.Equal([Path.GetFileName(archivePath)], Directory.EnumerateFiles(outputPath).Select(Path.GetFileName));
    }
 
    [Fact]
    public async Task BuildImageAsync_WithOutputPathAndLocalImageStore_SavesCallerTag()
    {
        var processRunner = new TestProcessRunner();
        processRunner.EnqueueResult();
        processRunner.EnqueueResult();
        var runtime = CreateRuntime(processRunner);
        var options = new ContainerImageBuildOptions
        {
            ImageName = "myapp",
            Tag = "aspire-layered-0123456789abcdef",
            OutputPath = "out",
            ImageFormat = ContainerImageFormat.Oci,
            RequiresLocalImageStore = true
        };
 
        await BuildImageAsync(runtime, options);
 
        // Callers that require the local image store already build under their own private tag and
        // rewrite the archive themselves, so the runtime saves exactly the tag it was given.
        Assert.Collection(
            processRunner.ProcessSpecs,
            build => Assert.Equal("myapp:aspire-layered-0123456789abcdef", GetBuildTag(build)),
            save => Assert.Equal(PodmanContainerRuntime.BuildSaveArguments("myapp:aspire-layered-0123456789abcdef", options), save.Arguments));
    }
 
    [Fact]
    public async Task BuildImageAsync_WhenBuildFails_RemovesPrivateImageWithoutSaving()
    {
        var processRunner = new TestProcessRunner();
        processRunner.EnqueueResult(exitCode: 17, output: ["build failed"]);
        var runtime = CreateRuntime(processRunner);
        var options = new ContainerImageBuildOptions
        {
            ImageName = "myapp",
            Tag = "latest",
            OutputPath = "out",
            ImageFormat = ContainerImageFormat.Oci
        };
 
        var exception = await Assert.ThrowsAsync<ProcessFailedException>(() => BuildImageAsync(runtime, options));
 
        Assert.Equal(17, exception.ExitCode);
        Assert.Contains("build failed", exception.Message);
        Assert.Collection(
            processRunner.ProcessSpecs,
            buildSpec => Assert.Equal("build", buildSpec.ArgumentList![0]),
            removeSpec => Assert.StartsWith("rmi \"myapp:aspire-archive-", Assert.IsType<string>(removeSpec.Arguments), StringComparison.Ordinal));
    }
 
    [Fact]
    public async Task BuildImageAsync_WhenSaveFails_ThrowsProcessFailedException()
    {
        using var workspace = TemporaryWorkspace.Create(output);
        var processRunner = new TestProcessRunner();
        processRunner.EnqueueResult();
        processRunner.EnqueueResult(exitCode: 23, output: ["save failed"]);
        var runtime = CreateRuntime(processRunner);
        var options = new ContainerImageBuildOptions
        {
            ImageName = "myapp",
            Tag = "latest",
            OutputPath = workspace.WorkspaceRoot.FullName,
            ImageFormat = ContainerImageFormat.Oci
        };
 
        var exception = await Assert.ThrowsAsync<ProcessFailedException>(() => BuildImageAsync(runtime, options));
 
        Assert.Equal(23, exception.ExitCode);
        Assert.Contains("save failed", exception.Message);
        Assert.Collection(
            processRunner.ProcessSpecs,
            buildSpec => Assert.Equal("build", buildSpec.ArgumentList![0]),
            saveSpec => Assert.StartsWith("save ", Assert.IsType<string>(saveSpec.Arguments), StringComparison.Ordinal),
            removeSpec => Assert.StartsWith("rmi ", Assert.IsType<string>(removeSpec.Arguments), StringComparison.Ordinal));
        Assert.Empty(Directory.EnumerateFileSystemEntries(workspace.WorkspaceRoot.FullName));
    }
 
    private static string GetBuildTag(ProcessSpec buildSpec)
    {
        var arguments = buildSpec.ArgumentList!.ToList();
        return arguments[arguments.IndexOf("--tag") + 1];
    }
 
    private static TestProcessRunner CreateSaveProcessRunner()
    {
        // Podman qualifies a familiar name such as "myapp" as "localhost/myapp" in the archives it saves.
        // The save command looks like:
        //   save --format "docker-archive" --output "<path>" "myapp:aspire-archive-<guid>"
        return new TestProcessRunner
        {
            RunCallback = spec =>
            {
                if (spec.Arguments is { } arguments && arguments.StartsWith("save ", StringComparison.Ordinal))
                {
                    var match = Regex.Match(arguments, "--output \"(?<path>[^\"]+)\" \"(?<image>[^\"]+)\"$");
                    TestContainerImageArchive.WriteDockerArchive(match.Groups["path"].Value, $"localhost/{match.Groups["image"].Value}", "layer");
                }
            }
        };
    }
 
    private static PodmanContainerRuntime CreateRuntime(TestProcessRunner processRunner)
        => new(NullLogger<PodmanContainerRuntime>.Instance, processRunner);
 
    private static Task BuildImageAsync(PodmanContainerRuntime runtime, ContainerImageBuildOptions options)
        => runtime.BuildImageAsync(
            contextPath: "context",
            dockerfilePath: "Dockerfile",
            options: options,
            buildArguments: [],
            buildSecrets: [],
            stage: null,
            cancellationToken: CancellationToken.None);
}