Skip to content

Commit 22bd4c6

Browse files
lbussellCopilot
andauthored
Add SignImagesCommand to ImageBuilder (#1957)
Related: - dotnet/dotnet-docker#4589 - #1376 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 37f076a commit 22bd4c6

37 files changed

Lines changed: 2235 additions & 417 deletions

src/Dockerfile.linux

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ RUN dotnet restore -r linux-$TARGETARCH ./ImageBuilder/Microsoft.DotNet.ImageBui
1616

1717
# copy everything else and publish
1818
COPY . ./
19-
RUN dotnet publish -r linux-$TARGETARCH ./ImageBuilder/Microsoft.DotNet.ImageBuilder.csproj --self-contained=true --no-restore -o out
19+
# MicroBuild signing requires running a DLL directly, so it is more size-efficient for ImageBuilder
20+
# to use the same runtime and ship as framework-dependent instead of self-contained.
21+
RUN dotnet publish -r linux-$TARGETARCH ./ImageBuilder/Microsoft.DotNet.ImageBuilder.csproj --no-restore -o out
2022

2123

2224
# build runtime image
23-
FROM mcr.microsoft.com/dotnet/runtime-deps:9.0-azurelinux3.0
25+
FROM mcr.microsoft.com/dotnet/runtime:9.0-azurelinux3.0
2426

2527
# install tooling
2628
RUN tdnf install -y \

src/ImageBuilder.Tests/BuildCommandTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3533,8 +3533,7 @@ private static BuildCommand CreateBuildCommand(
35333533
IManifestServiceFactory? manifestServiceFactory = null,
35343534
IRegistryCredentialsProvider? registryCredentialsProvider = null,
35353535
IAzureTokenCredentialProvider? azureTokenCredentialProvider = null,
3536-
IImageCacheService? imageCacheService = null,
3537-
IOptions<PublishConfiguration>? publishOptions = null)
3536+
IImageCacheService? imageCacheService = null)
35383537
{
35393538
BuildCommand command = new(
35403539
dockerService ?? Mock.Of<IDockerService>(),
@@ -3545,8 +3544,7 @@ private static BuildCommand CreateBuildCommand(
35453544
manifestServiceFactory ?? Mock.Of<IManifestServiceFactory>(),
35463545
registryCredentialsProvider ?? Mock.Of<IRegistryCredentialsProvider>(),
35473546
azureTokenCredentialProvider ?? Mock.Of<IAzureTokenCredentialProvider>(),
3548-
imageCacheService ?? Mock.Of<IImageCacheService>(),
3549-
publishOptions ?? CreateOptionsMock<PublishConfiguration>());
3547+
imageCacheService ?? Mock.Of<IImageCacheService>());
35503548

35513549
return command;
35523550
}

src/ImageBuilder.Tests/GenerateSigningPayloadsCommandTests.cs

Lines changed: 0 additions & 228 deletions
This file was deleted.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Text;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
11+
namespace Microsoft.DotNet.ImageBuilder.Tests.Helpers;
12+
13+
/// <summary>
14+
/// In-memory <see cref="IFileSystem"/> implementation for unit tests.
15+
/// Stores file contents in a dictionary and tracks all operations for assertions.
16+
/// </summary>
17+
internal sealed class InMemoryFileSystem : IFileSystem
18+
{
19+
private readonly Dictionary<string, byte[]> _files = [];
20+
private readonly HashSet<string> _directories = [];
21+
22+
/// <summary>
23+
/// Paths written via <see cref="WriteAllText"/> or <see cref="WriteAllTextAsync"/>.
24+
/// </summary>
25+
public List<string> FilesWritten { get; } = [];
26+
27+
/// <summary>
28+
/// Paths read via <see cref="ReadAllBytes"/>, <see cref="ReadAllBytesAsync"/>, or <see cref="ReadAllTextAsync"/>.
29+
/// </summary>
30+
public List<string> FilesRead { get; } = [];
31+
32+
/// <summary>
33+
/// Paths deleted via <see cref="DeleteFile"/>.
34+
/// </summary>
35+
public List<string> FilesDeleted { get; } = [];
36+
37+
/// <summary>
38+
/// Paths created via <see cref="CreateDirectory"/>.
39+
/// </summary>
40+
public List<string> DirectoriesCreated { get; } = [];
41+
42+
/// <summary>
43+
/// Seeds a file with text content before a test runs.
44+
/// </summary>
45+
public void AddFile(string path, string contents) =>
46+
_files[path] = Encoding.UTF8.GetBytes(contents);
47+
48+
/// <summary>
49+
/// Seeds a file with binary content before a test runs.
50+
/// </summary>
51+
public void AddFile(string path, byte[] contents) =>
52+
_files[path] = contents;
53+
54+
public void WriteAllText(string path, string contents)
55+
{
56+
_files[path] = Encoding.UTF8.GetBytes(contents);
57+
FilesWritten.Add(path);
58+
}
59+
60+
public Task WriteAllTextAsync(string path, string? contents, CancellationToken cancellationToken = default)
61+
{
62+
_files[path] = Encoding.UTF8.GetBytes(contents ?? string.Empty);
63+
FilesWritten.Add(path);
64+
return Task.CompletedTask;
65+
}
66+
67+
public byte[] ReadAllBytes(string path)
68+
{
69+
FilesRead.Add(path);
70+
return _files.TryGetValue(path, out var bytes)
71+
? bytes
72+
: throw new FileNotFoundException("File not found", path);
73+
}
74+
75+
public Task<byte[]> ReadAllBytesAsync(string path, CancellationToken cancellationToken = default) =>
76+
Task.FromResult(ReadAllBytes(path));
77+
78+
public Task<string> ReadAllTextAsync(string path, CancellationToken cancellationToken = default)
79+
{
80+
FilesRead.Add(path);
81+
return _files.TryGetValue(path, out var bytes)
82+
? Task.FromResult(Encoding.UTF8.GetString(bytes))
83+
: throw new FileNotFoundException("File not found", path);
84+
}
85+
86+
public bool FileExists(string path) => _files.ContainsKey(path);
87+
88+
public void DeleteFile(string path)
89+
{
90+
_files.Remove(path);
91+
FilesDeleted.Add(path);
92+
}
93+
94+
public DirectoryInfo CreateDirectory(string path)
95+
{
96+
_directories.Add(path);
97+
DirectoriesCreated.Add(path);
98+
return new DirectoryInfo(path);
99+
}
100+
101+
/// <summary>
102+
/// Gets the text content of a file, for test assertions.
103+
/// </summary>
104+
public string GetFileText(string path) =>
105+
Encoding.UTF8.GetString(_files[path]);
106+
}

0 commit comments

Comments
 (0)