-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
218 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
tests/Microsoft.DotNet.Docker.Tests/TestAppArtifacts/NLSTest.Dockerfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
ARG sdk_image | ||
ARG runtime_image | ||
|
||
FROM ${sdk_image} AS sdk | ||
RUN dotnet new console -n App -o /src --no-restore | ||
WORKDIR /src | ||
COPY NLSTest.cs /src/Program.cs | ||
RUN dotnet restore | ||
RUN dotnet publish --no-restore -o /app | ||
|
||
FROM ${runtime_image} AS runtime | ||
ARG icu_expected | ||
ENV ICU_EXPECTED=${icu_expected} | ||
COPY --from=sdk /app /app/ | ||
ENTRYPOINT ["/app/App"] |
87 changes: 87 additions & 0 deletions
87
tests/Microsoft.DotNet.Docker.Tests/TestAppArtifacts/NLSTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System.Globalization; | ||
using static System.Console; | ||
|
||
const string nlsVar = "DOTNET_SYSTEM_GLOBALIZATION_USENLS"; | ||
const string icuVar = "ICU_EXPECTED"; | ||
|
||
GetEnvironmentVariableValue(nlsVar); | ||
bool icuExpected = GetEnvironmentVariableValue(icuVar); | ||
|
||
bool icuMode = IsIcuMode(); | ||
WriteLine($"Detected ICU mode: {icuMode}"); | ||
|
||
if (icuMode != icuExpected) | ||
{ | ||
throw new Exception($"ICU mode detected as {icuMode}, expected {icuExpected}"); | ||
} | ||
|
||
// https://learn.microsoft.com/en-us/dotnet/core/extensions/globalization-icu#stringendswith | ||
Assert( | ||
""" "abc".EndsWith("\0")""", | ||
"abc".EndsWith("\0").ToString(), | ||
icuResult: true.ToString(), | ||
nlsResult: false.ToString(), | ||
icuMode); | ||
Assert( | ||
""" "abc".EndsWith("\0", StringComparison.CurrentCulture)""", | ||
"abc".EndsWith("\0", StringComparison.CurrentCulture).ToString(), | ||
icuResult: true.ToString(), | ||
nlsResult: false.ToString(), | ||
icuMode); | ||
|
||
// https://learn.microsoft.com/en-us/dotnet/core/extensions/globalization-icu#stringstartswith | ||
Assert( | ||
""" "foo".StartsWith("\0")""", | ||
"foo".StartsWith("\0").ToString(), | ||
icuResult: true.ToString(), | ||
nlsResult: false.ToString(), | ||
icuMode); | ||
Assert( | ||
""" "foo".StartsWith("\0", StringComparison.CurrentCulture)""", | ||
"foo".StartsWith("\0", StringComparison.CurrentCulture).ToString(), | ||
icuResult: true.ToString(), | ||
nlsResult: false.ToString(), | ||
icuMode); | ||
|
||
// https://learn.microsoft.com/en-us/dotnet/core/extensions/globalization-icu#stringindexof | ||
Assert( | ||
""" "Hel\0lo".IndexOf("\0")""", | ||
"Hel\0lo".IndexOf("\0").ToString(), | ||
icuResult: "0", | ||
nlsResult: "3", | ||
icuMode); | ||
Assert( | ||
""" "Hel\0lo".IndexOf("\0", StringComparison.CurrentCulture)""", | ||
"Hel\0lo".IndexOf("\0", StringComparison.CurrentCulture).ToString(), | ||
icuResult: "0", | ||
nlsResult: "3", | ||
icuMode); | ||
|
||
WriteLine("All assertions passed!"); | ||
|
||
// https://learn.microsoft.com/en-us/dotnet/core/extensions/globalization-icu#determine-if-your-app-is-using-icu | ||
bool IsIcuMode() | ||
{ | ||
SortVersion sortVersion = CultureInfo.InvariantCulture.CompareInfo.Version; | ||
byte[] bytes = sortVersion.SortId.ToByteArray(); | ||
int version = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0]; | ||
return version != 0 && version == sortVersion.FullVersion; | ||
} | ||
|
||
void Assert(string functionText, string actualResult, string icuResult, string nlsResult, bool icuExpected) | ||
{ | ||
string expectedResult = icuExpected ? icuResult : nlsResult; | ||
Console.WriteLine($"{functionText} returned {actualResult}"); | ||
if (actualResult != expectedResult) | ||
{ | ||
throw new Exception($"Assertion failed: {functionText} returned {actualResult}, expected {expectedResult}"); | ||
} | ||
} | ||
|
||
bool GetEnvironmentVariableValue(string variable) | ||
{ | ||
string value = Environment.GetEnvironmentVariable(variable); | ||
bool parsedValue = !string.IsNullOrWhiteSpace(value) && bool.Parse(value); | ||
WriteLine($"{variable} evaluated to {parsedValue}"); | ||
return parsedValue; | ||
} |
83 changes: 83 additions & 0 deletions
83
tests/Microsoft.DotNet.Docker.Tests/TestScenarios/NlsScenario.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
|
||
namespace Microsoft.DotNet.Docker.Tests.TestScenarios; | ||
|
||
#nullable enable | ||
public sealed class NlsScenario : ITestScenario, IDisposable | ||
{ | ||
private const string DockerfileName = "NLSTest.Dockerfile"; | ||
|
||
private const string TestSourceFileName = "NLSTest.cs"; | ||
|
||
private readonly TempFolderContext _tempFolderContext = FileHelper.UseTempFolder(); | ||
|
||
private readonly ProductImageData _imageData; | ||
|
||
private readonly DotNetImageRepo _repo; | ||
|
||
private readonly DockerHelper _dockerHelper; | ||
|
||
public NlsScenario( | ||
ProductImageData imageData, | ||
DotNetImageRepo repo, | ||
DockerHelper dockerHelper) | ||
{ | ||
_imageData = imageData; | ||
_repo = repo; | ||
_dockerHelper = dockerHelper; | ||
} | ||
|
||
// ICU is not supported on Nano Server | ||
private bool IsIcuSupported => _imageData.OS.Contains(OS.ServerCore); | ||
|
||
public Task ExecuteAsync() | ||
{ | ||
// Setup project in temp dir | ||
string dockerfilePath = Path.Combine(_tempFolderContext.Path, "Dockerfile"); | ||
File.Copy( | ||
sourceFileName: Path.Combine(DockerHelper.TestArtifactsDir, DockerfileName), | ||
destFileName: dockerfilePath); | ||
File.Copy( | ||
sourceFileName: Path.Combine(DockerHelper.TestArtifactsDir, TestSourceFileName), | ||
destFileName: Path.Combine(_tempFolderContext.Path, TestSourceFileName)); | ||
|
||
string sdkImage = _imageData.GetImage(DotNetImageRepo.SDK, _dockerHelper); | ||
string runtimeImage = _imageData.GetImage(_repo, _dockerHelper); | ||
string[] buildArgs = | ||
[ | ||
$"sdk_image={sdkImage}", | ||
$"runtime_image={runtimeImage}", | ||
]; | ||
|
||
if (IsIcuSupported) | ||
{ | ||
buildArgs = [..buildArgs, $"icu_expected={IsIcuSupported}"]; | ||
} | ||
|
||
string tag = nameof(NlsScenario).ToLowerInvariant(); | ||
_dockerHelper.Build( | ||
tag: tag, | ||
dockerfile: dockerfilePath, | ||
contextDir: _tempFolderContext.Path, | ||
pull: Config.PullImages, | ||
buildArgs: buildArgs); | ||
|
||
string containerName = ImageData.GenerateContainerName(nameof(NlsScenario)); | ||
Func<string> runImage = () => _dockerHelper.Run(tag, containerName); | ||
|
||
string justification = $"image {runtimeImage} should{(IsIcuSupported ? "" : " not")} support ICU"; | ||
runImage.Should().NotThrow(because: justification) | ||
.Which.Should().ContainEquivalentOf("All assertions passed", Exactly.Once()); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public void Dispose() => _tempFolderContext.Dispose(); | ||
} |
16 changes: 16 additions & 0 deletions
16
tests/Microsoft.DotNet.Docker.Tests/WindowsImageTheoryAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace Microsoft.DotNet.Docker.Tests; | ||
|
||
public class WindowsImageTheoryAttribute : DotNetTheoryAttribute | ||
{ | ||
public WindowsImageTheoryAttribute() | ||
{ | ||
if (DockerHelper.IsLinuxContainerModeEnabled) | ||
{ | ||
Skip = "Windows image test not applicable when running in Linux Container mode"; | ||
} | ||
} | ||
} |