Skip to content

Commit

Permalink
Add tests for architecture switch feature
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoRossignoli committed Jan 14, 2022
1 parent d39826a commit 71d8605
Show file tree
Hide file tree
Showing 10 changed files with 556 additions and 9 deletions.
2 changes: 1 addition & 1 deletion scripts/build/TestPlatform.Dependencies.props
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VSSdkBuildToolsVersion>17.0.1600</VSSdkBuildToolsVersion>
Expand Down
4 changes: 3 additions & 1 deletion scripts/common.lib.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ function Install-DotNetCli
$dotnetInstallPath = Join-Path $env:TP_TOOLS_DIR "dotnet"
New-Item -ItemType directory -Path $dotnetInstallPath -Force | Out-Null
& $dotnetInstallScript -Channel 6.0 -InstallDir $dotnetInstallPath -Version $env:DOTNET_CLI_VERSION


& $dotnetInstallScript -Channel 6.0 -InstallDir "${dotnetInstallPath}_x86" -Version $env:DOTNET_CLI_VERSION -Architecture x86 -NoPath

& $dotnetInstallScript -InstallDir "$dotnetInstallPath" -Runtime 'dotnet' -Version '2.1.30' -Channel '2.1' -Architecture x64 -NoPath
$env:DOTNET_ROOT= $dotnetInstallPath

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.TestPlatform.SmokeTests
{
using Microsoft.TestPlatform.TestUtilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.IO;
using System.Linq;

[TestClass]
// On Linux/Mac we don't download the same .NET SDK bundles
[TestCategory("Windows-Review")]
public class DotnetHostArchitectureVerifierTests : IntegrationTestBase
{
[TestMethod]
[DataRow("X64")]
[DataRow("X86")]
public void VerifyHostArchitecture(string architecture)
{
using (Workspace workSpace = new Workspace(GetResultsDirectory()))
{
string dotnetPath = GetDownloadedDotnetMuxerFromTools(architecture);
var vstestConsolePath = GetDotnetRunnerPath();
var dotnetRunnerPath = workSpace.CreateDirectory("dotnetrunner");
workSpace.CopyAll(new DirectoryInfo(Path.GetDirectoryName(vstestConsolePath)), dotnetRunnerPath);

// Patch the runner
string sdkVersion = GetLatestSdkVersion(dotnetPath);
string runtimeConfigFile = Path.Combine(dotnetRunnerPath.FullName, "vstest.console.runtimeconfig.json");
JObject patchRuntimeConfig = JObject.Parse(File.ReadAllText(runtimeConfigFile));
patchRuntimeConfig["runtimeOptions"]["framework"]["version"] = sdkVersion;
File.WriteAllText(runtimeConfigFile, patchRuntimeConfig.ToString());

var environmentVariables = new Dictionary<string, string>
{
["DOTNET_MULTILEVEL_LOOKUP"] = "0",
["ExpectedArchitecture"] = $"{architecture}"
};

this.ExecuteApplication(dotnetPath, "new mstest", out string stdOut, out string stdError, out int exitCode, environmentVariables, workSpace.Path);

// Patch test file
File.WriteAllText(Path.Combine(workSpace.Path, "UnitTest1.cs"),
@"
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
namespace cfebbc5339cf4c22854e79824e938c74;
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Assert.AreEqual(Environment.GetEnvironmentVariable(""ExpectedArchitecture""), System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture.ToString());
}
}");


this.ExecuteApplication(dotnetPath, $"test -p:VsTestConsolePath=\"{Path.Combine(dotnetRunnerPath.FullName, Path.GetFileName(vstestConsolePath))}\"", out stdOut, out stdError, out exitCode, environmentVariables, workSpace.Path);
Assert.AreEqual(0, exitCode, stdOut);
}
}

private string GetLatestSdkVersion(string dotnetPath)
=> Path.GetFileName(Directory.GetDirectories(Path.Combine(Path.GetDirectoryName(dotnetPath), @"shared/Microsoft.NETCore.App")).OrderByDescending(x => x).First());
}
}
36 changes: 31 additions & 5 deletions test/Microsoft.TestPlatform.TestUtilities/IntegrationTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class IntegrationTestBase
private readonly string XUnitTestAdapterRelativePath = @"xunit.runner.visualstudio\{0}\build\_common".Replace('\\', Path.DirectorySeparatorChar);
private readonly string ChutzpahTestAdapterRelativePath = @"chutzpah\{0}\tools".Replace('\\', Path.DirectorySeparatorChar);

protected readonly bool IsWindows = System.Environment.OSVersion.Platform.ToString().StartsWith("Win");
protected static readonly bool IsWindows = System.Environment.OSVersion.Platform.ToString().StartsWith("Win");

public enum UnitTestFramework
{
Expand Down Expand Up @@ -143,7 +143,6 @@ public void InvokeVsTest(string arguments)
this.FormatStandardOutCome();
}


/// <summary>
/// Invokes our local copy of dotnet that is patched with artifacts from the build with specified arguments.
/// </summary>
Expand Down Expand Up @@ -596,7 +595,7 @@ private void ExecutePatchedDotnet(string command, string args, out string stdOut
this.ExecuteApplication(patchedDotnetPath, string.Join(" ", command, args), out stdOut, out stdError, out exitCode, environmentVariables);
}

private void ExecuteApplication(string path, string args, out string stdOut, out string stdError, out int exitCode, Dictionary<string, string> environmentVariables = null)
protected void ExecuteApplication(string path, string args, out string stdOut, out string stdError, out int exitCode, Dictionary<string, string> environmentVariables = null, string workingDirectory = null)
{
if (string.IsNullOrWhiteSpace(path))
{
Expand All @@ -611,12 +610,17 @@ private void ExecuteApplication(string path, string args, out string stdOut, out
process.StartInfo.FileName = path;
process.StartInfo.Arguments = args;
process.StartInfo.UseShellExecute = false;
//vstestconsole.StartInfo.WorkingDirectory = testEnvironment.PublishDirectory;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
process.StartInfo.StandardErrorEncoding = Encoding.UTF8;

if (workingDirectory != null)
{
process.StartInfo.WorkingDirectory = workingDirectory;
}

if (environmentVariables != null)
{
foreach (var variable in environmentVariables)
Expand Down Expand Up @@ -756,13 +760,35 @@ protected static string GetResultsDirectory()
// AGENT_TEMPDIRECTORY is AzureDevops variable, which is set to path
// that is cleaned up after every job. This is preferable to use over
// just the normal temp.
var temp = Environment.GetEnvironmentVariable("AGENT_TEMPDIRECTORY") ?? Path.GetTempPath();
var temp = GetTempPath();
var directoryPath = Path.Combine(temp, Guid.NewGuid().ToString("n"));
Directory.CreateDirectory(directoryPath);

return directoryPath;
}

protected static string GetTempPath() => Environment.GetEnvironmentVariable("AGENT_TEMPDIRECTORY") ?? Path.GetTempPath();

protected static string GetDownloadedDotnetMuxerFromTools(string architecture)
{
if (architecture != "X86" && architecture != "X64")
{
throw new NotSupportedException(nameof(architecture));
}

string path = Path.Combine(IntegrationTestEnvironment.TestPlatformRootDirectory, "tools",
architecture == "X86" ?
"dotnet_x86" :
$"dotnet",
$"dotnet{(IsWindows ? ".exe" : "")}");

Assert.IsTrue(File.Exists(path));

return path;
}

protected static string GetDotnetRunnerPath() => Path.Combine(IntegrationTestEnvironment.TestPlatformRootDirectory, "artifacts", IntegrationTestEnvironment.BuildConfiguration, "netcoreapp2.1", "vstest.console.dll");

protected static void TryRemoveDirectory(string directory)
{
if (Directory.Exists(directory))
Expand Down
55 changes: 55 additions & 0 deletions test/Microsoft.TestPlatform.TestUtilities/Workspace.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.IO;

namespace Microsoft.TestPlatform.TestUtilities
{
public class Workspace : IDisposable
{
public Workspace(string path)
{
Path = path;
}

public string Path { get; }

public void Dispose()
{
if (!string.IsNullOrEmpty(Path))
{
try
{
if (Directory.Exists(Path))
Directory.Delete(Path, true);
}
catch
{
// ignore
}
}
}

public DirectoryInfo CreateDirectory(string dir) => Directory.CreateDirectory(System.IO.Path.Combine(Path, dir));

public void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
Directory.CreateDirectory(target.FullName);

// Copy each file into the new directory.
foreach (FileInfo fi in source.GetFiles())
{
fi.CopyTo(System.IO.Path.Combine(target.FullName, fi.Name), true);
}

// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
}
}
13 changes: 13 additions & 0 deletions test/TestAssets/ArchitectureSwitch/ArchitectureSwitch.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">net6.0;net5.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">net6.0;netcoreapp3.1</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0-preview-20211130-02" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.0.2" />
</ItemGroup>
</Project>
31 changes: 31 additions & 0 deletions test/TestAssets/ArchitectureSwitch/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace TestProjectNetcore
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
foreach(DictionaryEntry envVar in Environment.GetEnvironmentVariables())
{
if(envVar.Key.ToString().StartsWith("DOTNET_ROOT"))
{
Console.WriteLine($"{envVar.Key}: {envVar.Value.ToString()}");
}
}

Console.WriteLine("OSArchitecture: " + RuntimeInformation.OSArchitecture.ToString());
Console.WriteLine("ProcessArchitecture: " + RuntimeInformation.ProcessArchitecture.ToString());
Console.WriteLine("Runtime location: " + typeof(object).Assembly.Location);
Assert.IsTrue(false);
}
}
}
5 changes: 5 additions & 0 deletions test/TestAssets/ArchitectureSwitch/global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sdk": {
"version": "6.0.200-preview"
}
}
18 changes: 16 additions & 2 deletions test/TestAssets/TestAssets.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29102.190
# Visual Studio Version 17
VisualStudioVersion = 17.1.32024.52
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NUnitAdapterPerfTestProject", "PerfAssets\NUnitAdapterPerfTestProject\NUnitAdapterPerfTestProject.csproj", "{F22A8D65-0581-4CC7-9C1C-9BC9F9E80DA4}"
EndProject
Expand Down Expand Up @@ -92,6 +92,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ParametrizedTestProject", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AttachmentProcessorDataCollector", "AttachmentProcessorDataCollector\AttachmentProcessorDataCollector.csproj", "{16F51720-29D0-472A-93FA-2604D61991B7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ArchitectureSwitch", "ArchitectureSwitch\ArchitectureSwitch.csproj", "{452352E1-71CA-436E-8165-F284EE36C924}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -606,6 +608,18 @@ Global
{16F51720-29D0-472A-93FA-2604D61991B7}.Release|x64.Build.0 = Release|Any CPU
{16F51720-29D0-472A-93FA-2604D61991B7}.Release|x86.ActiveCfg = Release|Any CPU
{16F51720-29D0-472A-93FA-2604D61991B7}.Release|x86.Build.0 = Release|Any CPU
{452352E1-71CA-436E-8165-F284EE36C924}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{452352E1-71CA-436E-8165-F284EE36C924}.Debug|Any CPU.Build.0 = Debug|Any CPU
{452352E1-71CA-436E-8165-F284EE36C924}.Debug|x64.ActiveCfg = Debug|Any CPU
{452352E1-71CA-436E-8165-F284EE36C924}.Debug|x64.Build.0 = Debug|Any CPU
{452352E1-71CA-436E-8165-F284EE36C924}.Debug|x86.ActiveCfg = Debug|Any CPU
{452352E1-71CA-436E-8165-F284EE36C924}.Debug|x86.Build.0 = Debug|Any CPU
{452352E1-71CA-436E-8165-F284EE36C924}.Release|Any CPU.ActiveCfg = Release|Any CPU
{452352E1-71CA-436E-8165-F284EE36C924}.Release|Any CPU.Build.0 = Release|Any CPU
{452352E1-71CA-436E-8165-F284EE36C924}.Release|x64.ActiveCfg = Release|Any CPU
{452352E1-71CA-436E-8165-F284EE36C924}.Release|x64.Build.0 = Release|Any CPU
{452352E1-71CA-436E-8165-F284EE36C924}.Release|x86.ActiveCfg = Release|Any CPU
{452352E1-71CA-436E-8165-F284EE36C924}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 71d8605

Please sign in to comment.