Skip to content

Commit 0845901

Browse files
committed
Green up tests
1 parent 99d39f7 commit 0845901

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

src/Containers/Microsoft.NET.Build.Containers/KnownStrings.cs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public static class Properties
2727

2828
public static readonly string ComputeContainerBaseImage = nameof(ComputeContainerBaseImage);
2929
public static readonly string ComputeContainerConfig = nameof(ComputeContainerConfig);
30+
public static readonly string _ComputeContainerExecutionArgs = nameof(_ComputeContainerExecutionArgs);
3031
public static readonly string AssemblyName = nameof(AssemblyName);
3132
public static readonly string ContainerBaseRegistry = nameof(ContainerBaseRegistry);
3233
public static readonly string ContainerBaseName = nameof(ContainerBaseName);

src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets

+21-19
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@
4141
<ContainerRuntimeIdentifier Condition="'$(ContainerRuntimeIdentifier)' == '' and '$(RuntimeIdentifier)' != ''">$(RuntimeIdentifier)</ContainerRuntimeIdentifier>
4242
<ContainerRuntimeIdentifiers Condition="'$(BuildingInsideVisualStudio)' != 'true' and '$(ContainerRuntimeIdentifiers)' == '' and '$(IsRidAgnostic)' != 'true'">$(RuntimeIdentifiers)</ContainerRuntimeIdentifiers>
4343
<ContainerRuntimeIdentifier Condition="'$(ContainerRuntimeIdentifier)' == '' and '$(ContainerRuntimeIdentifiers)' == ''">linux-$(NETCoreSdkPortableRuntimeIdentifier.Split('-')[1])</ContainerRuntimeIdentifier>
44-
45-
<_ContainerIsUsingMicrosoftDefaultImages Condition="'$(ContainerBaseImage)' == ''">true</_ContainerIsUsingMicrosoftDefaultImages>
46-
<_ContainerIsUsingMicrosoftDefaultImages Condition="'$(ContainerBaseImage)' != ''">false</_ContainerIsUsingMicrosoftDefaultImages>
4744
</PropertyGroup>
4845

4946
<ItemGroup>
@@ -99,20 +96,6 @@
9996
<!-- Only default a tag name if no tag names at all are provided -->
10097
<ContainerImageTag Condition="'$(ContainerImageTag)' == '' and '$(ContainerImageTags)' == ''">latest</ContainerImageTag>
10198
<ContainerImageTag Condition="'$(AutoGenerateImageTag)' == 'true' and '$(ContainerImageTags)' == ''">$([System.DateTime]::UtcNow.ToString('yyyyMMddhhmmss'))</ContainerImageTag>
102-
103-
<!-- The Container RID should default to the RID used for the entire build (to ensure things run on the platform they are built for), but the user knows best and so should be able to set it explicitly.
104-
For builds that have a RID, we default to that RID. Otherwise, we default to the Linux RID matching the architecture of the currently-executing SDK. -->
105-
<_ContainerIsTargetingWindows>false</_ContainerIsTargetingWindows>
106-
<_ContainerIsTargetingWindows Condition="$(ContainerRuntimeIdentifier.StartsWith('win'))">true</_ContainerIsTargetingWindows>
107-
108-
<!-- Set the WorkingDirectory depending on the RID -->
109-
<ContainerWorkingDirectory Condition="'$(ContainerWorkingDirectory)' == '' and !$(_ContainerIsTargetingWindows)">/app/</ContainerWorkingDirectory>
110-
<ContainerWorkingDirectory Condition="'$(ContainerWorkingDirectory)' == '' and $(_ContainerIsTargetingWindows)">C:\app\</ContainerWorkingDirectory>
111-
</PropertyGroup>
112-
113-
<!-- We only set a default user when the base image is Microsoft-authored, and we're targeting a version of those images that supports a nonroot user -->
114-
<PropertyGroup Label="ContainerUser Assignment" Condition="$(_ContainerIsUsingMicrosoftDefaultImages) and $(_ContainerIsTargetingNet8TFM) and '$(ContainerUser)' == ''">
115-
<ContainerUser Condition="$(_ContainerIsTargetingWindows)">ContainerUser</ContainerUser>
11699
</PropertyGroup>
117100

118101
<ParseContainerProperties FullyQualifiedBaseImageName="$(ContainerBaseImage)"
@@ -199,7 +182,28 @@
199182
</PublishContainerDependsOn>
200183
</PropertyGroup>
201184

185+
<!-- These args are relevant to container execution and are per-RID by nature. Therefore they're a direct dependency of the _PublishSingleContainer
186+
target and not computed at the outer layer. -->
202187
<Target Name="_ComputeContainerExecutionArgs">
188+
<PropertyGroup>
189+
<!-- The Container RID should default to the RID used for the entire build (to ensure things run on the platform they are built for), but the user knows best and so should be able to set it explicitly.
190+
For builds that have a RID, we default to that RID. Otherwise, we default to the Linux RID matching the architecture of the currently-executing SDK. -->
191+
<_ContainerIsTargetingWindows>false</_ContainerIsTargetingWindows>
192+
<_ContainerIsTargetingWindows Condition="$(ContainerRuntimeIdentifier.StartsWith('win'))">true</_ContainerIsTargetingWindows>
193+
194+
<!-- Set the WorkingDirectory depending on the RID -->
195+
<ContainerWorkingDirectory Condition="'$(ContainerWorkingDirectory)' == '' and !$(_ContainerIsTargetingWindows)">/app/</ContainerWorkingDirectory>
196+
<ContainerWorkingDirectory Condition="'$(ContainerWorkingDirectory)' == '' and $(_ContainerIsTargetingWindows)">C:\app\</ContainerWorkingDirectory>
197+
198+
<_ContainerIsUsingMicrosoftDefaultImages Condition="'$(ContainerBaseImage)' == ''">true</_ContainerIsUsingMicrosoftDefaultImages>
199+
<_ContainerIsUsingMicrosoftDefaultImages Condition="'$(ContainerBaseImage)' != ''">false</_ContainerIsUsingMicrosoftDefaultImages>
200+
</PropertyGroup>
201+
202+
<!-- We only set a default user when the base image is Microsoft-authored, and we're targeting a version of those images that supports a nonroot user -->
203+
<PropertyGroup Label="ContainerUser Assignment" Condition="$(_ContainerIsUsingMicrosoftDefaultImages) and $(_ContainerIsTargetingNet8TFM) and '$(ContainerUser)' == ''">
204+
<ContainerUser Condition="$(_ContainerIsTargetingWindows)">ContainerUser</ContainerUser>
205+
</PropertyGroup>
206+
203207
<ItemGroup Label="AppCommand Assignment" Condition="'$(ContainerAppCommandInstruction)' != 'None'">
204208
<!-- For self-contained, invoke the native executable as a single arg -->
205209
<ContainerAppCommand Condition="@(ContainerAppCommand->Count()) == 0 and $(_ContainerIsSelfContained)" Include="$(ContainerWorkingDirectory)$(AssemblyName)$(_NativeExecutableExtension)" />
@@ -326,11 +330,9 @@
326330
ContainerRegistry=$(ContainerRegistry);
327331
_ContainerImageTags=@(ContainerImageTags, ';');
328332
ContainerRepository=$(ContainerRepository);
329-
ContainerWorkingDirectory=$(ContainerWorkingDirectory);
330333
_ContainerLabel=@(ContainerLabel->'%(Identity):%(Value)');
331334
_ContainerPort=@(ContainerPort->'%(Identity):%(Type)');
332335
_ContainerEnvironmentVariables=@(ContainerEnvironmentVariable->'%(Identity):%(Value)');
333-
ContainerUser=$(ContainerUser);
334336
ContainerGenerateLabels=$(ContainerGenerateLabels);
335337
ContainerGenerateLabelsImageBaseDigest=$(ContainerGenerateLabelsImageBaseDigest);
336338
_SkipContainerPublishing=$(_SkipContainerPublishing)

test/Microsoft.NET.Build.Containers.IntegrationTests/TargetsTests.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public void CanDeferContainerAppCommand(
2626
}, projectName: $"{nameof(CanDeferContainerAppCommand)}_{prop}_{value}_{string.Join("_", expectedAppCommandArgs)}");
2727
using var _ = d;
2828
var instance = project.CreateProjectInstance(ProjectInstanceSettings.None);
29-
//Assert.True(instance.Build([ ComputeContainerConfig ], []));
30-
instance.Build([ ComputeContainerConfig ], []);
29+
instance.Build([ _ComputeContainerExecutionArgs ], []);
3130
var computedAppCommand = instance.GetItems(ContainerAppCommand).Select(i => i.EvaluatedInclude);
3231

3332
// The test was not testing anything previously, as the list returned was zero length,
@@ -36,7 +35,6 @@ public void CanDeferContainerAppCommand(
3635
computedAppCommand.Should().BeEquivalentTo(expectedAppCommandArgs);
3736
}
3837

39-
4038
public static TheoryData<string, string, bool, string[]> ContainerAppCommands()
4139
{
4240
char s = Path.DirectorySeparatorChar;
@@ -316,7 +314,7 @@ public void CanComputeContainerUser(string tfm, string rid, string? expectedUser
316314
}, projectName: $"{nameof(CanComputeContainerUser)}_{tfm}_{rid}_{expectedUser}");
317315
using var _ = d;
318316
var instance = project.CreateProjectInstance(ProjectInstanceSettings.None);
319-
instance.Build(new[] { ComputeContainerConfig }, new[] { logger }, null, out var outputs).Should().BeTrue(String.Join(Environment.NewLine, logger.Errors));
317+
instance.Build(new[] { _ComputeContainerExecutionArgs }, new[] { logger }, null, out var outputs).Should().BeTrue(String.Join(Environment.NewLine, logger.Errors));
320318
var computedTag = instance.GetProperty("ContainerUser")?.EvaluatedValue;
321319
computedTag.Should().Be(expectedUser);
322320
}

0 commit comments

Comments
 (0)