Skip to content

Replace RunTests scripts with .NET app #20337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions eng/helix/content/RunTests/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<Project>
</Project>
2 changes: 2 additions & 0 deletions eng/helix/content/RunTests/Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<Project>
</Project>
20 changes: 20 additions & 0 deletions eng/helix/content/RunTests/ProcessResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 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 RunTests
{
public class ProcessResult
{
public ProcessResult(string standardOutput, string standardError, int exitCode)
{
StandardOutput = standardOutput;
StandardError = standardError;
ExitCode = exitCode;
}

public string StandardOutput { get; }
public string StandardError { get; }
public int ExitCode { get; }
}
}
156 changes: 156 additions & 0 deletions eng/helix/content/RunTests/ProcessUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// 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.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

#nullable enable

namespace RunTests
{
public static class ProcessUtil
{
[DllImport("libc", SetLastError = true, EntryPoint = "kill")]
private static extern int sys_kill(int pid, int sig);

public static async Task<ProcessResult> RunAsync(
string filename,
string arguments,
string? workingDirectory = null,
bool throwOnError = true,
IDictionary<string, string?>? environmentVariables = null,
Action<string>? outputDataReceived = null,
Action<string>? errorDataReceived = null,
Action<int>? onStart = null,
CancellationToken cancellationToken = default)
{
using var process = new Process()
{
StartInfo =
{
FileName = filename,
Arguments = arguments,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
},
EnableRaisingEvents = true
};


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

if (environmentVariables != null)
{
foreach (var kvp in environmentVariables)
{
process.StartInfo.Environment.Add(kvp);
}
}

var outputBuilder = new StringBuilder();
process.OutputDataReceived += (_, e) =>
{
if (e.Data != null)
{
if (outputDataReceived != null)
{
outputDataReceived.Invoke(e.Data);
}
else
{
outputBuilder.AppendLine(e.Data);
}
}
};

var errorBuilder = new StringBuilder();
process.ErrorDataReceived += (_, e) =>
{
if (e.Data != null)
{
if (errorDataReceived != null)
{
errorDataReceived.Invoke(e.Data);
}
else
{
errorBuilder.AppendLine(e.Data);
}
}
};

var processLifetimeTask = new TaskCompletionSource<ProcessResult>();

process.Exited += (_, e) =>
{
if (throwOnError && process.ExitCode != 0)
{
processLifetimeTask.TrySetException(new InvalidOperationException($"Command {filename} {arguments} returned exit code {process.ExitCode}"));
}
else
{
processLifetimeTask.TrySetResult(new ProcessResult(outputBuilder.ToString(), errorBuilder.ToString(), process.ExitCode));
}
};

process.Start();
onStart?.Invoke(process.Id);

process.BeginOutputReadLine();
process.BeginErrorReadLine();

var cancelledTcs = new TaskCompletionSource<object?>();
await using var _ = cancellationToken.Register(() => cancelledTcs.TrySetResult(null));

var result = await Task.WhenAny(processLifetimeTask.Task, cancelledTcs.Task);

if (result == cancelledTcs.Task)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
sys_kill(process.Id, sig: 2); // SIGINT

var cancel = new CancellationTokenSource();

await Task.WhenAny(processLifetimeTask.Task, Task.Delay(TimeSpan.FromSeconds(5), cancel.Token));

cancel.Cancel();
}

if (!process.HasExited)
{
process.CloseMainWindow();

if (!process.HasExited)
{
process.Kill();
}
}
}

return await processLifetimeTask.Task;
}

public static void KillProcess(int pid)
{
try
{
using var process = Process.GetProcessById(pid);
process?.Kill();
}
catch (ArgumentException) { }
catch (InvalidOperationException) { }
}
}
}
188 changes: 188 additions & 0 deletions eng/helix/content/RunTests/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace RunTests
{
class Program
{
static async Task Main(string[] args)
{
if (args.Length < 7)
{
Console.WriteLine($"Expected at least 7 args, got {args.Length}.");
Environment.Exit(1);
return;
}

var target = args[0];
var sdkVersion = args[1];
var runtimeVersion = args[2];
var helixQueue = args[3];
var architecture = args[4];
var quarantined = args[5];
var efVersion = args[6];
var HELIX_WORKITEM_ROOT = Environment.GetEnvironmentVariable("HELIX_WORKITEM_ROOT");

var path = Environment.GetEnvironmentVariable("PATH");
var dotnetRoot = Environment.GetEnvironmentVariable("DOTNET_ROOT");

// Rename default.NuGet.config to NuGet.config if there is not a custom one from the project
if (!File.Exists("NuGet.config"))
{
File.Copy("default.NuGet.config", "NuGet.config");
}

var environmentVariables = new Dictionary<string, string>();
environmentVariables.Add("PATH", path);
environmentVariables.Add("DOTNET_ROOT", dotnetRoot);
environmentVariables.Add("helix", helixQueue);

Console.WriteLine($"Current Directory: {HELIX_WORKITEM_ROOT}");
var helixDir = Environment.GetEnvironmentVariable("HELIX_WORKITEM_ROOT");
Console.WriteLine($"Setting HELIX_DIR: {helixDir}");
environmentVariables.Add("HELIX_DIR", helixDir);
environmentVariables.Add("NUGET_FALLBACK_PACKAGES", helixDir);
var nugetRestore = Path.Combine(helixDir, "nugetRestore");
Console.WriteLine($"Creating nuget restore directory: {nugetRestore}");
environmentVariables.Add("NUGET_RESTORE", nugetRestore);
var dotnetEFFullPath = Path.Combine(nugetRestore, $"dotnet-ef/{efVersion}/tools/netcoreapp3.1/any/dotnet-ef.exe");
Console.WriteLine($"Set DotNetEfFullPath: {dotnetEFFullPath}");
environmentVariables.Add("DotNetEfFullPath", dotnetEFFullPath);

Console.WriteLine("Checking for Microsoft.AspNetCore.App");
if (Directory.Exists("Microsoft.AspNetCore.App"))
{
Console.WriteLine($"Found Microsoft.AspNetCore.App, copying to {dotnetRoot}/shared/Microsoft.AspNetCore.App/{runtimeVersion}");
foreach (var file in Directory.EnumerateFiles("Microsoft.AspNetCore.App", "*.*", SearchOption.AllDirectories))
{
File.Copy(file, $"{dotnetRoot}/shared/Microsoft.AspNetCore.App/{runtimeVersion}", overwrite: true);
}

Console.WriteLine($"Adding current directory to nuget sources: {HELIX_WORKITEM_ROOT}");

await ProcessUtil.RunAsync($"{dotnetRoot}/dotnet",
$"nuget add source {HELIX_WORKITEM_ROOT} --configfile NuGet.config",
environmentVariables: environmentVariables);

await ProcessUtil.RunAsync($"{dotnetRoot}/dotnet",
"nuget add source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json --configfile NuGet.config",
environmentVariables: environmentVariables);

await ProcessUtil.RunAsync($"{dotnetRoot}/dotnet",
"nuget list source",
environmentVariables: environmentVariables);

await ProcessUtil.RunAsync($"{dotnetRoot}/dotnet",
$"tool install dotnet-ef --global --version {efVersion}",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Global?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We set the DOTNET_CLI_HOME environment variable so it isn't "global". But maybe I should move some more of that logic out of the scripts

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

environmentVariables: environmentVariables);

path += $";{Environment.GetEnvironmentVariable("DOTNET_CLI_HOME")}/.dotnet/tools";
environmentVariables["PATH"] = path;
}

Directory.CreateDirectory(nugetRestore);

// Rename default.runner.json to xunit.runner.json if there is not a custom one from the project
if (!File.Exists("xunit.runner.json"))
{
File.Copy("default.runner.json", "xunit.runner.json");
}

Console.WriteLine();
Console.WriteLine("Displaying directory contents");
foreach (var file in Directory.EnumerateFiles("./"))
{
Console.WriteLine(Path.GetFileName(file));
}
Console.WriteLine();

var discoveryResult = await ProcessUtil.RunAsync($"{dotnetRoot}/dotnet",
$"vstest {target} -lt",
environmentVariables: environmentVariables);

if (discoveryResult.StandardOutput.Contains("Exception thrown"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would this happen?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ever in stderr?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with the specifics of this
#8107

{
Console.WriteLine("Exception thrown during test discovery.");
Console.WriteLine(discoveryResult.StandardOutput);
Environment.Exit(1);
return;
}

var exitCode = 0;
var commonTestArgs = $"vstest {target} --logger:xunit --logger:\"console;verbosity=normal\" --blame";
if (string.Equals(quarantined, "true") || string.Equals(quarantined, "1"))
{
Console.WriteLine("Running quarantined tests.");

// Filter syntax: https://github.com/Microsoft/vstest-docs/blob/master/docs/filter.md

var result = await ProcessUtil.RunAsync($"{dotnetRoot}/dotnet",
commonTestArgs + " --TestCaseFilter:\"Quarantined=true\"",
environmentVariables: environmentVariables,
outputDataReceived: Console.WriteLine,
errorDataReceived: Console.WriteLine,
throwOnError: false);

if (result.ExitCode != 0)
{
Console.WriteLine($"Failure in quarantined tests. Exit code: {result.ExitCode}.");
}
}
else
{
Console.WriteLine("Running non-quarantined tests.");

// Filter syntax: https://github.com/Microsoft/vstest-docs/blob/master/docs/filter.md
var result = await ProcessUtil.RunAsync($"{dotnetRoot}/dotnet",
commonTestArgs + " --TestCaseFilter:\"Quarantined!=true\"",
environmentVariables: environmentVariables,
outputDataReceived: Console.WriteLine,
errorDataReceived: Console.WriteLine,
throwOnError: false);

if (result.ExitCode != 0)
{
Console.WriteLine($"Failure in non-quarantined tests. Exit code: {result.ExitCode}.");
exitCode = result.ExitCode;
}
}

Console.WriteLine();
if (File.Exists("TestResults/TestResults.xml"))
{
Console.WriteLine("Copying TestResults/TestResults.xml to ./testResults.xml");
File.Copy("TestResults/TestResults.xml", "testResults.xml");
}
else
{
Console.WriteLine("No test results found.");
}

var HELIX_WORKITEM_UPLOAD_ROOT = Environment.GetEnvironmentVariable("HELIX_WORKITEM_UPLOAD_ROOT");
Console.WriteLine($"Copying artifacts/log/ to {HELIX_WORKITEM_UPLOAD_ROOT}/");
if (Directory.Exists("artifacts/log"))
{
foreach (var file in Directory.EnumerateFiles("artifacts/log", "*.log", SearchOption.AllDirectories))
{
// Combine the directory name + log name for the copied log file name to avoid overwriting duplicate test names in different test projects
var logName = $"{Path.GetFileName(Path.GetDirectoryName(file))}_{Path.GetFileName(file)}";
Console.WriteLine($"Copying: {file} to {Path.Combine(HELIX_WORKITEM_UPLOAD_ROOT, logName)}");
File.Copy(file, Path.Combine(HELIX_WORKITEM_UPLOAD_ROOT, logName));
File.Copy(file, Path.Combine(HELIX_WORKITEM_UPLOAD_ROOT, "..", logName));
}
}
else
{
Console.WriteLine("No logs found in artifacts/log");
}

Console.WriteLine("Completed Helix job.");
Environment.Exit(exitCode);
}
}
}
8 changes: 8 additions & 0 deletions eng/helix/content/RunTests/RunTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp5.0</TargetFramework>
</PropertyGroup>

</Project>
Loading