|
| 1 | +using System.Formats.Tar; |
| 2 | +using System.Text.Json; |
| 3 | +using Workleap.DotNet.CodingStandards.Tests.Helpers; |
| 4 | +using Xunit.Abstractions; |
| 5 | + |
| 6 | +namespace Workleap.DotNet.CodingStandards.Tests; |
| 7 | + |
| 8 | +public sealed class ContainerTests(PackageFixture fixture, ITestOutputHelper testOutputHelper) : IClassFixture<PackageFixture> |
| 9 | +{ |
| 10 | + [Fact] |
| 11 | + public async Task PublishContainer_WithContainerUseNativeCommand_EntrypointUsesNativeExecutable() |
| 12 | + { |
| 13 | + using var project = new ProjectBuilder(fixture, testOutputHelper); |
| 14 | + |
| 15 | + var archivePath = Path.Combine(project.RootFolder, "container"); |
| 16 | + |
| 17 | + project.AddFile("test.csproj", $""" |
| 18 | + <Project Sdk="Microsoft.NET.Sdk.Web"> |
| 19 | + <PropertyGroup> |
| 20 | + <TargetFramework>net$(NETCoreAppMaximumVersion)</TargetFramework> |
| 21 | + <RuntimeIdentifier>linux-x64</RuntimeIdentifier> |
| 22 | + <ImplicitUsings>enable</ImplicitUsings> |
| 23 | + <Nullable>enable</Nullable> |
| 24 | + <ContainerArchiveOutputPath>{archivePath}</ContainerArchiveOutputPath> |
| 25 | + <ErrorLog>{ProjectBuilder.SarifFileName},version=2.1</ErrorLog> |
| 26 | + </PropertyGroup> |
| 27 | +
|
| 28 | + <ItemGroup> |
| 29 | + <PackageReference Include="Workleap.DotNet.CodingStandards" Version="*" /> |
| 30 | + </ItemGroup> |
| 31 | + </Project> |
| 32 | + """); |
| 33 | + |
| 34 | + project.AddFile("Program.cs", |
| 35 | + """ |
| 36 | + var builder = WebApplication.CreateBuilder(args); |
| 37 | + var app = builder.Build(); |
| 38 | + app.MapGet("/health", () => "ok"); |
| 39 | + app.Run(); |
| 40 | + """ + "\n"); |
| 41 | + |
| 42 | + var (publishExitCode, _) = await project.ExecuteDotnetCommand(["publish", "/t:PublishContainer"]); |
| 43 | + Assert.Equal(0, publishExitCode); |
| 44 | + |
| 45 | + var entrypoint = await GetEntrypointFromArchive(archivePath); |
| 46 | + testOutputHelper.WriteLine("Entrypoint: " + JsonSerializer.Serialize(entrypoint)); |
| 47 | + |
| 48 | + Assert.Equal(["/app/test"], entrypoint); |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public async Task PublishContainer_WithContainerUseNativeCommandDisabled_EntrypointUsesDotnet() |
| 53 | + { |
| 54 | + using var project = new ProjectBuilder(fixture, testOutputHelper); |
| 55 | + |
| 56 | + var archivePath = Path.Combine(project.RootFolder, "container"); |
| 57 | + |
| 58 | + project.AddFile("test.csproj", $""" |
| 59 | + <Project Sdk="Microsoft.NET.Sdk.Web"> |
| 60 | + <PropertyGroup> |
| 61 | + <TargetFramework>net$(NETCoreAppMaximumVersion)</TargetFramework> |
| 62 | + <RuntimeIdentifier>linux-x64</RuntimeIdentifier> |
| 63 | + <ImplicitUsings>enable</ImplicitUsings> |
| 64 | + <Nullable>enable</Nullable> |
| 65 | + <ContainerUseNativeCommand>false</ContainerUseNativeCommand> |
| 66 | + <ContainerArchiveOutputPath>{archivePath}</ContainerArchiveOutputPath> |
| 67 | + <ErrorLog>{ProjectBuilder.SarifFileName},version=2.1</ErrorLog> |
| 68 | + </PropertyGroup> |
| 69 | +
|
| 70 | + <ItemGroup> |
| 71 | + <PackageReference Include="Workleap.DotNet.CodingStandards" Version="*" /> |
| 72 | + </ItemGroup> |
| 73 | + </Project> |
| 74 | + """); |
| 75 | + |
| 76 | + project.AddFile("Program.cs", |
| 77 | + """ |
| 78 | + var builder = WebApplication.CreateBuilder(args); |
| 79 | + var app = builder.Build(); |
| 80 | + app.MapGet("/health", () => "ok"); |
| 81 | + app.Run(); |
| 82 | + """ + "\n"); |
| 83 | + |
| 84 | + var (publishExitCode, _) = await project.ExecuteDotnetCommand(["publish", "/t:PublishContainer"]); |
| 85 | + Assert.Equal(0, publishExitCode); |
| 86 | + |
| 87 | + var entrypoint = await GetEntrypointFromArchive(archivePath); |
| 88 | + testOutputHelper.WriteLine("Entrypoint: " + JsonSerializer.Serialize(entrypoint)); |
| 89 | + |
| 90 | + Assert.Equal(["dotnet", "/app/test.dll"], entrypoint); |
| 91 | + } |
| 92 | + |
| 93 | + private static async Task<string[]> GetEntrypointFromArchive(string archiveDirectory) |
| 94 | + { |
| 95 | + var tarGzFile = Directory.GetFiles(archiveDirectory, "*.tar.gz").Single(); |
| 96 | + |
| 97 | + // Read only the JSON metadata entries from the archive (stop before layer blobs) |
| 98 | + var jsonFiles = new Dictionary<string, string>(StringComparer.Ordinal); |
| 99 | + await using var fileStream = File.OpenRead(tarGzFile); |
| 100 | + await using var tarReader = new TarReader(fileStream); |
| 101 | + |
| 102 | + while (await tarReader.GetNextEntryAsync() is { } entry) |
| 103 | + { |
| 104 | + if (entry.DataStream == null || !entry.Name.EndsWith(".json", StringComparison.OrdinalIgnoreCase)) |
| 105 | + { |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + using var reader = new StreamReader(entry.DataStream, leaveOpen: true); |
| 110 | + jsonFiles[entry.Name] = await reader.ReadToEndAsync(); |
| 111 | + } |
| 112 | + |
| 113 | + // Parse Docker manifest -> config |
| 114 | + using var manifest = JsonDocument.Parse(jsonFiles["manifest.json"]); |
| 115 | + var configFileName = manifest.RootElement[0].GetProperty("Config").GetString()!; |
| 116 | + |
| 117 | + using var config = JsonDocument.Parse(jsonFiles[configFileName]); |
| 118 | + |
| 119 | + return config.RootElement |
| 120 | + .GetProperty("config") |
| 121 | + .GetProperty("Entrypoint") |
| 122 | + .EnumerateArray() |
| 123 | + .Select(e => e.GetString()!) |
| 124 | + .ToArray(); |
| 125 | + } |
| 126 | +} |
0 commit comments