-
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.
Add globalization scenario test (#6012)
- Loading branch information
Showing
5 changed files
with
150 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
70 changes: 70 additions & 0 deletions
70
tests/Microsoft.DotNet.Docker.Tests/TestAppArtifacts/GlobalizationTest.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,70 @@ | ||
using System.Globalization; | ||
using static System.Console; | ||
|
||
const string envVarName = "DOTNET_SYSTEM_GLOBALIZATION_INVARIANT"; | ||
string envVarValue = Environment.GetEnvironmentVariable(envVarName) ?? "null"; | ||
bool invariantModeEnvVarSetting = envVarValue == "true" || envVarValue == "1"; | ||
bool invariantModeEnabled = IsInvariantModeEnabled(); | ||
|
||
string isEnabledString = $"Globalization invariant mode is {(invariantModeEnabled ? "enabled" : "disabled")}"; | ||
WriteLine(isEnabledString); | ||
|
||
if (invariantModeEnabled != invariantModeEnvVarSetting) | ||
{ | ||
throw new Exception("Environment variable mis-match: " + isEnabledString | ||
+ $", but {envVarName} is set to `{envVarValue}`, which evaluates to {invariantModeEnvVarSetting}."); | ||
} | ||
|
||
try | ||
{ | ||
WriteLine($"The following should {(invariantModeEnabled ? "" : "not ")}produce an exception:"); | ||
TestGlobalizationFunctionality(); | ||
TestTimeZoneFunctionality(); | ||
if (invariantModeEnabled) | ||
{ | ||
throw new Exception("Expected an exception when testing globalization functionality but one did not occur."); | ||
} | ||
} | ||
catch (CultureNotFoundException) | ||
{ | ||
if (invariantModeEnabled) | ||
{ | ||
WriteLine("Successfully caught a CultureNotFoundException, invariant mode is working as expected."); | ||
} | ||
else | ||
{ | ||
throw; | ||
} | ||
} | ||
|
||
WriteLine("Globalization test succeeded"); | ||
|
||
void TestGlobalizationFunctionality() | ||
{ | ||
const int Value = 1337; | ||
WriteLine($"Value: {Value}"); | ||
WriteLine($" en-US: {Value.ToString("c", new CultureInfo("en-US"))}"); | ||
WriteLine($" jp-JP: {Value.ToString("c", new CultureInfo("jp-JP"))}"); | ||
} | ||
|
||
void TestTimeZoneFunctionality() | ||
{ | ||
DateTime localTime = DateTime.Now; | ||
TimeZoneInfo pacificZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"); | ||
DateTime pacificTime = TimeZoneInfo.ConvertTime(localTime, pacificZone); | ||
WriteLine("Local Time: " + localTime); | ||
WriteLine("Pacific Time: " + pacificTime); | ||
} | ||
|
||
// https://stackoverflow.com/a/75299176 | ||
bool IsInvariantModeEnabled() | ||
{ | ||
try | ||
{ | ||
return CultureInfo.GetCultureInfo("en-US").NumberFormat.CurrencySymbol == "¤"; | ||
} | ||
catch (CultureNotFoundException) | ||
{ | ||
return true; | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
tests/Microsoft.DotNet.Docker.Tests/TestScenarios/GlobalizationScenario.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,76 @@ | ||
// 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 GlobalizationScenario : ITestScenario, IDisposable | ||
{ | ||
private const string Dockerfile = """ | ||
ARG sdk_image | ||
ARG runtime_image | ||
|
||
FROM ${sdk_image} AS sdk | ||
RUN dotnet new console -n App -o /src --no-restore | ||
WORKDIR /src | ||
COPY Program.cs /src/Program.cs | ||
RUN dotnet restore | ||
RUN dotnet publish --no-restore -o /app | ||
|
||
FROM ${runtime_image} AS runtime | ||
COPY --from=sdk /app /app/ | ||
ENTRYPOINT ["/app/App"] | ||
"""; | ||
|
||
private readonly TempFolderContext _tempFolderContext = FileHelper.UseTempFolder(); | ||
private readonly ProductImageData _imageData; | ||
private readonly DotNetImageRepo _repo; | ||
private readonly DockerHelper _dockerHelper; | ||
|
||
public GlobalizationScenario( | ||
ProductImageData imageData, | ||
DotNetImageRepo repo, | ||
DockerHelper dockerHelper) | ||
{ | ||
_imageData = imageData; | ||
_repo = repo; | ||
_dockerHelper = dockerHelper; | ||
} | ||
|
||
public async Task ExecuteAsync() | ||
{ | ||
// Setup project in temp dir | ||
string dockerfilePath = Path.Combine(_tempFolderContext.Path, "Dockerfile"); | ||
await File.WriteAllTextAsync(path: dockerfilePath, contents: Dockerfile); | ||
|
||
File.Copy( | ||
sourceFileName: Path.Combine(DockerHelper.TestArtifactsDir, "GlobalizationTest.cs"), | ||
destFileName: Path.Combine(_tempFolderContext.Path, "Program.cs")); | ||
|
||
string tag = nameof(GlobalizationScenario).ToLowerInvariant(); | ||
_dockerHelper.Build( | ||
tag: tag, | ||
dockerfile: dockerfilePath, | ||
contextDir: _tempFolderContext.Path, | ||
pull: Config.PullImages, | ||
buildArgs: | ||
[ | ||
$"sdk_image={_imageData.GetImage(DotNetImageRepo.SDK, _dockerHelper)}", | ||
$"runtime_image={_imageData.GetImage(_repo, _dockerHelper)}", | ||
]); | ||
|
||
string containerName = ImageData.GenerateContainerName(nameof(GlobalizationScenario)); | ||
string output = _dockerHelper.Run(tag, containerName); | ||
|
||
output.Should().NotBeNullOrWhiteSpace(); | ||
output.Should().ContainEquivalentOf("Globalization test succeeded", Exactly.Once()); | ||
} | ||
|
||
public void Dispose() => _tempFolderContext.Dispose(); | ||
} |