From e5c80d9b32b70ec57ae1aefb19e9b33b20f42bda 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 50458adc2a37..ddce84f70e45 100644 --- a/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets +++ b/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets @@ -400,7 +400,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 d139aa0397160bfb50516497ba440ccc724abb78 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 | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs index 3d9e24147556..1da2ef72f733 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs @@ -780,6 +780,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)); From 8e67d79ac2fbf24e1e36dcec594f850aaa7d5be5 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 ddce84f70e45..6c26890d49be 100644 --- a/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets +++ b/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets @@ -298,9 +298,9 @@ @@ -400,7 +400,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/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs index 1da2ef72f733..34d438634b7e 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs @@ -785,8 +785,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"); @@ -814,10 +813,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 @@ -825,8 +823,8 @@ public void MultiArchStillAllowsSingleRID() _testOutput, "--rm", "--name", - $"test-container-{imageName}-x64", - imageX64) + $"test-container-{imageName}", + qualifiedImageName) .Execute(); processResultX64.Should().Pass().And.HaveStdOut("Hello, World!"); @@ -839,8 +837,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"); @@ -853,7 +850,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}", @@ -868,8 +865,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 @@ -878,7 +874,7 @@ public void MultiArchStillAllowsSingleRIDUsingJustRIDProperties() "--rm", "--name", $"test-container-{imageName}-x64", - imageX64) + qualifiedImageName) .Execute(); processResultX64.Should().Pass().And.HaveStdOut("Hello, World!"); From 7276bcc568bdd480e77d07558fafd4d613ca10bd 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 6c26890d49be..81a5901cf86d 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"); [DockerIsAvailableAndSupportsArchFact("linux/arm64")] public void EndToEndMultiArch_ArchivePublishing() From 88436e7a17398c3f9054a1db5423da6395b9ebab 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 81a5901cf86d..2ec6d1664af5 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/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs b/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs index 874da42a6cd2..82c7bfabec72 100644 --- a/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs +++ b/test/Microsoft.NET.Build.Containers.IntegrationTests/EndToEndTests.cs @@ -1278,7 +1278,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}",