From ca7f5481aa70131599d7aa6e0950aa078cbd319f Mon Sep 17 00:00:00 2001 From: Chet Husk Date: Thu, 16 Jan 2025 16:20:28 -0600 Subject: [PATCH 1/5] Fix typo in condition check for container publishing that led to inadvertend multi-arch publishing --- .../packaging/build/Microsoft.NET.Build.Containers.targets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets b/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets index 1b6ad491c8af..8332145ee29d 100644 --- a/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets +++ b/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets @@ -399,7 +399,7 @@ Condition="'$(IsPublishable)' == 'true' AND '$(EnableSdkContainerSupport)' == 'true'"> <_IsMultiTFMBuild Condition="'$(TargetFrameworks)' != '' and '$(TargetFramework)' == ''">true - <_IsMultiRIDBuild Condition="'$(BuildingInsideVisualStudio)' != 'true' and (('$(RuntimeIdentifiers)' != '' and '$(RuntimeIdentifier)' == '') or ('$(ContainerRuntimeIdentifiers)' != '' and '$(ContainerRuntimeIdentfier)' == ''))">true + <_IsMultiRIDBuild Condition="'$(BuildingInsideVisualStudio)' != 'true' and (('$(RuntimeIdentifiers)' != '' and '$(RuntimeIdentifier)' == '') or ('$(ContainerRuntimeIdentifiers)' != '' and '$(ContainerRuntimeIdentifier)' == ''))">true <_IsSingleRIDBuild Condition="'$(_IsMultiRIDBuild)' == ''">true From 56701a1afe85ff0d3f959d578a41d9ff8a31e32e Mon Sep 17 00:00:00 2001 From: Chet Husk Date: Thu, 16 Jan 2025 16:56:22 -0600 Subject: [PATCH 2/5] Add two new failing tests detailing scenarios that should work: * using Container-specific properties to target a single RID * using normal RID properties to target a single RID --- .../EndToEndTests.cs | 116 +++++++++++++++++- 1 file changed, 111 insertions(+), 5 deletions(-) diff --git a/src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs b/src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs index c78cf0eb8d5b..2dac6823b586 100644 --- a/src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs +++ b/src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs @@ -181,7 +181,7 @@ public async Task ApiEndToEndWithArchiveWritingAndLoad() [DockerAvailableFact] public async Task TarballsHaveCorrectStructure() - { + { var archiveFile = Path.Combine(TestSettings.TestArtifactsDirectory, nameof(TarballsHaveCorrectStructure), "app.tar.gz"); @@ -190,7 +190,7 @@ public async Task TarballsHaveCorrectStructure() await BuildDockerImageWithArciveDestinationAsync(archiveFile, ["latest"], nameof(TarballsHaveCorrectStructure)); await destinationReference.LocalRegistry!.LoadAsync(dockerImage, sourceReference, destinationReference, default).ConfigureAwait(false); - + Assert.True(File.Exists(archiveFile), $"File.Exists({archiveFile})"); CheckDockerTarballStructure(archiveFile); @@ -778,6 +778,112 @@ public void EndToEndMultiArch_LocalRegistry() newProjectDir.Delete(true); } + [DockerAvailableFact] + public void MultiArchStillAllowsSingleRID() + { + string imageName = NewImageName(); + string imageTag = "1.0"; + string imageX64 = $"{imageName}:{imageTag}-linux-x64"; + string imageArm64 = $"{imageName}:{imageTag}-linux-arm64"; + + // Create a new console project + DirectoryInfo newProjectDir = CreateNewProject("console"); + + // Run PublishContainer for multi-arch-capable, but single-arch actual + CommandResult commandResult = new DotnetCommand( + _testOutput, + "publish", + "/t:PublishContainer", + // make it so the app is _able_ to target both linux TFMs + "/p:RuntimeIdentifiers=\"linux-x64;linux-arm64\"", + // and that it opts into to multi-targeting containers for both of those linux TFMs + "/p:ContainerRuntimeIdentifiers=\"linux-x64;linux-arm64\"", + // but then only actually publishes for one of them + "/p:ContainerRuntimeIdentifier=linux-x64", + $"/p:ContainerBaseImage={DockerRegistryManager.FullyQualifiedBaseImageAspNet}", + $"/p:ContainerRepository={imageName}", + $"/p:ContainerImageTag={imageTag}", + "/p:EnableSdkContainerSupport=true", + "/bl") + .WithWorkingDirectory(newProjectDir.FullName) + .Execute(); + + // Check that the app was published for each RID, + // images were created locally for each RID + // and image index was NOT created + commandResult.Should().Pass() + .And.HaveStdOutContaining(GetPublishArtifactsPath(newProjectDir.FullName, "linux-x64")) + .And.NotHaveStdOutContaining(GetPublishArtifactsPath(newProjectDir.FullName, "linux-arm64")) + .And.HaveStdOutContaining($"Pushed image '{imageX64}' to local registry") + .And.NotHaveStdOutContaining($"Pushed image '{imageArm64}' to local registry") + .And.NotHaveStdOutContaining("Pushed image index"); + + // Check that the containers can be run + CommandResult processResultX64 = ContainerCli.RunCommand( + _testOutput, + "--rm", + "--name", + $"test-container-{imageName}-x64", + imageX64) + .Execute(); + processResultX64.Should().Pass().And.HaveStdOut("Hello, World!"); + + // Cleanup + newProjectDir.Delete(true); + } + + [DockerAvailableFact] + public void MultiArchStillAllowsSingleRIDUsingJustRIDProperties() + { + string imageName = NewImageName(); + string imageTag = "1.0"; + string imageX64 = $"{imageName}:{imageTag}-linux-x64"; + string imageArm64 = $"{imageName}:{imageTag}-linux-arm64"; + + // Create a new console project + DirectoryInfo newProjectDir = CreateNewProject("console"); + + // Run PublishContainer for multi-arch-capable, but single-arch actual + CommandResult commandResult = new DotnetCommand( + _testOutput, + "publish", + "/t:PublishContainer", + // make it so the app is _able_ to target both linux TFMs + "/p:RuntimeIdentifiers=\"linux-x64;linux-arm64\"", + // but then only actually publishes for one of them + "-r linux-x64", + $"/p:ContainerBaseImage={DockerRegistryManager.FullyQualifiedBaseImageAspNet}", + $"/p:ContainerRepository={imageName}", + $"/p:ContainerImageTag={imageTag}", + "/p:EnableSdkContainerSupport=true", + "/bl") + .WithWorkingDirectory(newProjectDir.FullName) + .Execute(); + + // Check that the app was published for each RID, + // images were created locally for each RID + // and image index was NOT created + commandResult.Should().Pass() + .And.HaveStdOutContaining(GetPublishArtifactsPath(newProjectDir.FullName, "linux-x64")) + .And.NotHaveStdOutContaining(GetPublishArtifactsPath(newProjectDir.FullName, "linux-arm64")) + .And.HaveStdOutContaining($"Pushed image '{imageX64}' to local registry") + .And.NotHaveStdOutContaining($"Pushed image '{imageArm64}' to local registry") + .And.NotHaveStdOutContaining("Pushed image index"); + + // Check that the containers can be run + CommandResult processResultX64 = ContainerCli.RunCommand( + _testOutput, + "--rm", + "--name", + $"test-container-{imageName}-x64", + imageX64) + .Execute(); + processResultX64.Should().Pass().And.HaveStdOut("Hello, World!"); + + // Cleanup + newProjectDir.Delete(true); + } + private DirectoryInfo CreateNewProject(string template, [CallerMemberName] string callerMemberName = "") { DirectoryInfo newProjectDir = new DirectoryInfo(Path.Combine(TestSettings.TestArtifactsDirectory, callerMemberName)); @@ -839,8 +945,8 @@ public void EndToEndMultiArch_ArchivePublishing() .And.HaveStdOutContaining($"Pushed image '{imageArm64}' to local archive at '{imageArm64Tarball}'") .And.NotHaveStdOutContaining("Pushed image index"); - // Check that tarballs were created - File.Exists(imageX64Tarball).Should().BeTrue(); + // Check that tarballs were created + File.Exists(imageX64Tarball).Should().BeTrue(); File.Exists(imageArm64Tarball).Should().BeTrue(); // Load the images from the tarballs @@ -886,7 +992,7 @@ public void EndToEndMultiArch_RemoteRegistry() // Create a new console project DirectoryInfo newProjectDir = CreateNewProject("console"); - + // Run PublishContainer for multi-arch with ContainerRegistry CommandResult commandResult = new DotnetCommand( _testOutput, From ca7ee82a5f1c40567c0dfbff4b4169355e80f782 Mon Sep 17 00:00:00 2001 From: Chet Husk Date: Thu, 16 Jan 2025 18:12:09 -0600 Subject: [PATCH 3/5] Green up one of the two tests by making the _IsMultiRIDBuild condition more correct --- .../Microsoft.NET.Build.Containers.targets | 15 ++++++++---- .../EndToEndTests.cs | 24 ++++++++----------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets b/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets index 8332145ee29d..868614abe354 100644 --- a/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets +++ b/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets @@ -297,9 +297,9 @@ @@ -399,7 +399,14 @@ Condition="'$(IsPublishable)' == 'true' AND '$(EnableSdkContainerSupport)' == 'true'"> <_IsMultiTFMBuild Condition="'$(TargetFrameworks)' != '' and '$(TargetFramework)' == ''">true - <_IsMultiRIDBuild Condition="'$(BuildingInsideVisualStudio)' != 'true' and (('$(RuntimeIdentifiers)' != '' and '$(RuntimeIdentifier)' == '') or ('$(ContainerRuntimeIdentifiers)' != '' and '$(ContainerRuntimeIdentifier)' == ''))">true + + <_HasCRIDsAndNoCRID Condition="'$(ContainerRuntimeIdentifiers)' != '' and '$(ContainerRuntimeIdentifier)' == ''">true + <_HasRIDs Condition="'$(RuntimeIdentifiers)' != ''">true + <_NoCRIDsOrCRIDorRID Condition="'$(ContainerRuntimeIdentifiers)' == '' and '$(ContainerRuntimeIdentifier)' == '' and '$(RuntimeIdentifier)' == ''">true + <_IsMultiRIDBuild Condition="'$(BuildingInsideVisualStudio)' != 'true' and ('$(_HasCRIDsAndNoCRID)' == true or ('$(_HasRIDs)' == 'true' and '$(_NoCRIDsOrCRIDorRID)' == 'true'))">true <_IsSingleRIDBuild Condition="'$(_IsMultiRIDBuild)' == ''">true diff --git a/src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs b/src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs index 2dac6823b586..f60c0bff20dc 100644 --- a/src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs +++ b/src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs @@ -783,8 +783,7 @@ public void MultiArchStillAllowsSingleRID() { string imageName = NewImageName(); string imageTag = "1.0"; - string imageX64 = $"{imageName}:{imageTag}-linux-x64"; - string imageArm64 = $"{imageName}:{imageTag}-linux-arm64"; + string qualifiedImageName = $"{imageName}:{imageTag}"; // Create a new console project DirectoryInfo newProjectDir = CreateNewProject("console"); @@ -812,10 +811,9 @@ public void MultiArchStillAllowsSingleRID() // images were created locally for each RID // and image index was NOT created commandResult.Should().Pass() - .And.HaveStdOutContaining(GetPublishArtifactsPath(newProjectDir.FullName, "linux-x64")) - .And.NotHaveStdOutContaining(GetPublishArtifactsPath(newProjectDir.FullName, "linux-arm64")) - .And.HaveStdOutContaining($"Pushed image '{imageX64}' to local registry") - .And.NotHaveStdOutContaining($"Pushed image '{imageArm64}' to local registry") + // no rid-specific path because we didn't pass RuntimeIdentifier + .And.NotHaveStdOutContaining(GetPublishArtifactsPath(newProjectDir.FullName, "linux-x64")) + .And.HaveStdOutContaining($"Pushed image '{qualifiedImageName}' to local registry") .And.NotHaveStdOutContaining("Pushed image index"); // Check that the containers can be run @@ -823,8 +821,8 @@ public void MultiArchStillAllowsSingleRID() _testOutput, "--rm", "--name", - $"test-container-{imageName}-x64", - imageX64) + $"test-container-{imageName}", + qualifiedImageName) .Execute(); processResultX64.Should().Pass().And.HaveStdOut("Hello, World!"); @@ -837,8 +835,7 @@ public void MultiArchStillAllowsSingleRIDUsingJustRIDProperties() { string imageName = NewImageName(); string imageTag = "1.0"; - string imageX64 = $"{imageName}:{imageTag}-linux-x64"; - string imageArm64 = $"{imageName}:{imageTag}-linux-arm64"; + string qualifiedImageName = $"{imageName}:{imageTag}"; // Create a new console project DirectoryInfo newProjectDir = CreateNewProject("console"); @@ -851,7 +848,7 @@ public void MultiArchStillAllowsSingleRIDUsingJustRIDProperties() // make it so the app is _able_ to target both linux TFMs "/p:RuntimeIdentifiers=\"linux-x64;linux-arm64\"", // but then only actually publishes for one of them - "-r linux-x64", + "-r", "linux-x64", $"/p:ContainerBaseImage={DockerRegistryManager.FullyQualifiedBaseImageAspNet}", $"/p:ContainerRepository={imageName}", $"/p:ContainerImageTag={imageTag}", @@ -866,8 +863,7 @@ public void MultiArchStillAllowsSingleRIDUsingJustRIDProperties() commandResult.Should().Pass() .And.HaveStdOutContaining(GetPublishArtifactsPath(newProjectDir.FullName, "linux-x64")) .And.NotHaveStdOutContaining(GetPublishArtifactsPath(newProjectDir.FullName, "linux-arm64")) - .And.HaveStdOutContaining($"Pushed image '{imageX64}' to local registry") - .And.NotHaveStdOutContaining($"Pushed image '{imageArm64}' to local registry") + .And.HaveStdOutContaining($"Pushed image '{qualifiedImageName}' to local registry") .And.NotHaveStdOutContaining("Pushed image index"); // Check that the containers can be run @@ -876,7 +872,7 @@ public void MultiArchStillAllowsSingleRIDUsingJustRIDProperties() "--rm", "--name", $"test-container-{imageName}-x64", - imageX64) + qualifiedImageName) .Execute(); processResultX64.Should().Pass().And.HaveStdOut("Hello, World!"); From 81b3df5c7fb54a30fbafe6c60e9a233a3332b057 Mon Sep 17 00:00:00 2001 From: Chet Husk Date: Thu, 16 Jan 2025 18:23:37 -0600 Subject: [PATCH 4/5] Fix second test problem by more carefully considering the order of single/multi rid checking --- .../build/Microsoft.NET.Build.Containers.targets | 8 ++++---- .../EndToEndTests.cs | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets b/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets index 868614abe354..5a45aa87a8be 100644 --- a/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets +++ b/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets @@ -34,19 +34,19 @@ - $(RuntimeIdentifiers) - $(RuntimeIdentifier) + $(RuntimeIdentifier) linux-$(NETCoreSdkPortableRuntimeIdentifier.Split('-')[1]) + $(RuntimeIdentifiers) <_ContainerIsUsingMicrosoftDefaultImages Condition="'$(ContainerBaseImage)' == ''">true <_ContainerIsUsingMicrosoftDefaultImages Condition="'$(ContainerBaseImage)' != ''">false - <_TargetRuntimeIdentifiers Include="$(ContainerRuntimeIdentifiers)" Condition="'$(ContainerRuntimeIdentifiers)' != ''" /> - <_TargetRuntimeIdentifiers Include="$(ContainerRuntimeIdentifier)" Condition="'$(ContainerRuntimeIdentifiers)' == ''" /> + <_TargetRuntimeIdentifiers Include="$(ContainerRuntimeIdentifier)" Condition="'$(ContainerRuntimeIdentifier)' != ''" /> + <_TargetRuntimeIdentifiers Include="$(ContainerRuntimeIdentifiers)" Condition="@(_TargetRuntimeIdentifiers->Count()) == 0" /> Path.Combine(projectDir, "bin", "Debug", ToolsetInfo.CurrentTargetFramework, rid, "publish"); + private string GetPublishArtifactsPath(string projectDir, string rid, string configuration = "Debug") + => Path.Combine(projectDir, "bin", configuration, ToolsetInfo.CurrentTargetFramework, rid, "publish"); [DockerSupportsArchFact("linux/arm64")] public void EndToEndMultiArch_ArchivePublishing() From 44a65477553df8aba3de97b77bb85e63384bcc20 Mon Sep 17 00:00:00 2001 From: Chet Husk Date: Thu, 16 Jan 2025 23:02:08 -0600 Subject: [PATCH 5/5] fix ordering to make sure that we only ever _write_ a default value after we've checked all possible user-provided values --- .../packaging/build/Microsoft.NET.Build.Containers.targets | 2 +- .../EndToEndTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets b/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets index 5a45aa87a8be..2a006d665a6b 100644 --- a/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets +++ b/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets @@ -37,8 +37,8 @@ $(RuntimeIdentifier) - linux-$(NETCoreSdkPortableRuntimeIdentifier.Split('-')[1]) $(RuntimeIdentifiers) + linux-$(NETCoreSdkPortableRuntimeIdentifier.Split('-')[1]) <_ContainerIsUsingMicrosoftDefaultImages Condition="'$(ContainerBaseImage)' == ''">true <_ContainerIsUsingMicrosoftDefaultImages Condition="'$(ContainerBaseImage)' != ''">false diff --git a/src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs b/src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs index 943a30026cbb..3b0015a10f0a 100644 --- a/src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs +++ b/src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs @@ -1276,7 +1276,7 @@ public void EndToEndMultiArch_Labels() // Run PublishContainer for multi-arch with ContainerGenerateLabels new DotnetCommand( _testOutput, - "build", + "publish", "/t:PublishContainer", "/p:RuntimeIdentifiers=\"linux-x64;linux-arm64\"", $"/p:ContainerBaseImage={DockerRegistryManager.FullyQualifiedBaseImageAspNet}",