Skip to content

Commit

Permalink
add SDK and TFM labels to generated images
Browse files Browse the repository at this point in the history
  • Loading branch information
baronfel committed Mar 5, 2024
1 parent 0f2c8df commit 75b10b8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
<ContainerGenerateLabelsImageTitle Condition="'$(ContainerGenerateLabelsImageTitle)' == ''">true</ContainerGenerateLabelsImageTitle>
<ContainerGenerateLabelsImageBaseDigest Condition="'$(ContainerGenerateLabelsImageBaseDigest)' == ''">true</ContainerGenerateLabelsImageBaseDigest>
<ContainerGenerateLabelsImageBaseName Condition="'$(ContainerGenerateLabelsImageBaseName)' == ''">true</ContainerGenerateLabelsImageBaseName>
<ContainerGenerateLabelsDotnetToolset Condition="'$(ContainerGenerateLabelsDotnetToolset)' == ''"></ContainerGenerateLabelsDotnetToolset>
</PropertyGroup>

<PropertyGroup Label="Defaults for Container Labels">
Expand All @@ -166,6 +167,8 @@
<!-- Need to compute digests, not just names, before we can light this up. This suggests we need a task where all of the 'read' steps are done. -->
<!-- <ContainerLabel Condition="'$(ContainerGenerateLabelsImageBaseDigest)' == 'true' and '$(ContainerBaseImageDigest)' != ''" Include="org.opencontainers.image.base.digest" Value="$(ContainerBaseImageDigest)" /> -->
<ContainerLabel Condition="'$(ContainerGenerateLabelsImageBaseName)' == 'true' and '$(ContainerBaseImage)' != ''" Include="org.opencontainers.image.base.name" Value="$(ContainerBaseImage)" />
<ContainerLabel Condition="'$(ContainerGenerateLabelsDotnetToolset)' == 'true' and '$(TargetFrameworkIdentifier)' == '.NETCoreApp'" Include="net.dot.runtime.majorminor" Value="$(_TargetFrameworkVersionWithoutV)"/>
<ContainerLabel Condition="'$(ContainerGenerateLabelsDotnetToolset)' == 'true'" Include="net.dot.sdk.version" Value="$(NETCoreSdkVersion)"/>
</ItemGroup>

<!-- These sourcelink-derived properties are only allowed to flow to generated artifacts if `PublishRepositoryUrl` is set as a user signal for opt-in.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static (Project, CapturingLogger, IDisposable) InitProject(Dictionary<str
props["TargetFramework"] = "net7.0";
props["_NativeExecutableExtension"] = ".exe"; //TODO: windows/unix split here
props["Version"] = "1.0.0"; // TODO: need to test non-compliant version strings here
props["NetCoreSdkVersion"] = "7.0.100"; // TODO: float this to current SDK?
props["NETCoreSdkVersion"] = "7.0.100"; // TODO: float this to current SDK?
// test setup parameters so that we can load the props/targets/tasks
props["ContainerCustomTasksAssembly"] = Path.GetFullPath(Path.Combine(".", "Microsoft.NET.Build.Containers.dll"));
props["_IsTest"] = "true";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,39 @@ public void ShouldIncludeBaseImageLabelsUnlessUserOptsOut(bool includeBaseImageL
};
}

[InlineData(true)]
[InlineData(false)]
[Theory]
public void ShouldIncludeSDKAndRuntimeVersionLabelsUnlessUserOptsOut(bool includeToolsetVersionLabels)
{
var runtimeMajorMinor = "7.0";
var randomSdkVersion = "8.0.100";
var expectedBaseImage = $"mcr.microsoft.com/dotnet/runtime:{runtimeMajorMinor}";
var (project, logger, d) = ProjectInitializer.InitProject(new()
{
["ContainerGenerateLabelsDotnetToolset"] = includeToolsetVersionLabels.ToString(),
["ContainerBaseImage"] = expectedBaseImage,
["ContainerGenerateLabels"] = true.ToString(), // always include other labels, but not necessarily the toolset labels
["NETCoreSdkVersion"] = randomSdkVersion // not functionally relevant for the test, just need a known version
}, projectName: $"{nameof(ShouldIncludeSDKAndRuntimeVersionLabelsUnlessUserOptsOut)}_{includeToolsetVersionLabels}");
using var _ = d;
var instance = project.CreateProjectInstance(global::Microsoft.Build.Execution.ProjectInstanceSettings.None);
instance.Build(new[] { ComputeContainerConfig }, new[] { logger }, null, out var outputs).Should().BeTrue("Build should have succeeded but failed due to {0}", String.Join("\n", logger.AllMessages));
var labels = instance.GetItems(ContainerLabel);
if (includeToolsetVersionLabels)
{
labels.Should().NotBeEmpty("Should have evaluated some labels by default")
.And.ContainSingle(label => LabelMatch("net.dot.runtime.majorminor", runtimeMajorMinor, label))
.And.ContainSingle(label => LabelMatch("net.dot.sdk.version", randomSdkVersion, label));
}
else
{
labels.Should().NotBeEmpty("Should have evaluated some labels by default")
.And.NotContain(label => LabelMatch("net.dot.runtime.majorminor", runtimeMajorMinor, label))
.And.ContainSingle(label => LabelMatch("net.dot.sdk.version", randomSdkVersion, label));
};
}

[InlineData("7.0.100", "v7.0", "7.0")]
[InlineData("7.0.100-preview.7", "v7.0", "7.0")]
[InlineData("7.0.100-rc.1", "v7.0", "7.0")]
Expand Down

0 comments on commit 75b10b8

Please sign in to comment.