Skip to content

Commit

Permalink
[TestFramework] Added prompt to install the test-proxy tool (Azure#36292
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kinelski authored Jun 2, 2023
1 parent 7cbf543 commit 29de8a2
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 11 deletions.
14 changes: 14 additions & 0 deletions eng/scripts/Install-TestProxyTool.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

Write-Host "New test recordings detected but the required test-proxy tool could not be found."
Write-Host "You must install it to push new recordings to the assets repository.`n"

Write-Host "This can be done manually by following the instructions at:"
Write-Host "https://github.com/Azure/azure-sdk-tools/blob/main/tools/test-proxy/Azure.Sdk.Tools.TestProxy/README.md#installation`n"

$run = Read-Host "Would you like to install the test-proxy tool now? [y/n]"
if ($run -eq 'y')
{
dotnet tool update azure.sdk.tools.testproxy --global --add-source https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json --version "1.0.0-dev*"
}
31 changes: 30 additions & 1 deletion sdk/core/Azure.Core.TestFramework/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,36 @@ In addition to the auto-rerecording functionality, using the RecordedTestAttribu

### Recording

When tests are run in `Record` mode, session records are saved to the project directory automatically in a folder named 'SessionRecords'.
Because of the quick growth of the repo size due to the presence of recordings, currently there is an ongoing effort to migrate them to the [Azure SDK Assets](https://github.com/Azure/azure-sdk-assets) repo. The location where session records are stored in your machine depends on whether migration already took place for your project or not.

For projects whose recordings have not been migrated yet, when tests are run in `Record` mode, session records are saved to the project directory automatically in a folder named 'SessionRecords'. The recordings contained in this folder must be pushed normally.

For projects whose recordings have already been migrated, when tests are run in `Record` mode, session records are saved in a local folder named '.assets', located at the root of this repo. This folder will be created automatically by the Test Framework and should not be committed with other changes. Instead, recordings must be pushed manually to the Azure SDK Assets repo with the help of the `test-proxy` command line tool.

To differentiate between the two types of projects, you just need to look for an `assets.json` file at your package directory. The file is only present if migration has taken place.

#### Installing the test-proxy tool

This step is only relevant if your project had its recordings migrated to the Azure SDK Assets repo.

In order to push new session records, you must have the `test-proxy` command line tool installed. It can be installed automatically when running the Test Framework in `Record` mode on Windows. You can check the installed version by invoking:
```PowerShell
test-proxy --version
```

If you need to install the `test-proxy` tool manually, check [Azure SDK Tools Test Proxy
](https://github.com/Azure/azure-sdk-tools/blob/main/tools/test-proxy/Azure.Sdk.Tools.TestProxy/README.md#installation) for installation options.

#### Pushing session records and updating assets.json

This step is only relevant if your project had its recordings migrated to the Azure SDK Assets repo.

The `assets.json` file located at your package directory is used by the Test Framework to figure out how to retrieve session records from the assets repo. In order to push new session records, you need to invoke:
```PowerShell
test-proxy push -a <path-to-assets.json>
```

On completion of the push, a newly created tag will be stamped into the `assets.json` file. This new tag must be committed and pushed to your package directory along with any other changes.

### Sanitizing

Expand Down
81 changes: 79 additions & 2 deletions sdk/core/Azure.Core.TestFramework/src/RecordedTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using Azure.Core.TestFramework.Models;
using Castle.DynamicProxy;
using NUnit.Framework;
Expand All @@ -37,6 +35,10 @@ public abstract class RecordedTestBase : ClientTestBase
(char)31, ':', '*', '?', '\\', '/'
});

private static readonly object s_syncLock = new();

private static bool s_ranTestProxyValidation;

private TestProxy _proxy;

private DateTime _testStartTime;
Expand Down Expand Up @@ -449,6 +451,11 @@ public virtual async Task StopTestRecordingAsync()
if (Recording != null)
{
await Recording.DisposeAsync(save);

if (Mode == RecordedTestMode.Record && save)
{
AssertTestProxyToolIsInstalled();
}
}

_proxy?.CheckForErrors();
Expand Down Expand Up @@ -513,6 +520,76 @@ public static Task Delay(RecordedTestMode mode, int milliseconds = 1000, int? pl
return Task.CompletedTask;
}

private void AssertTestProxyToolIsInstalled()
{
if (s_ranTestProxyValidation ||
TestEnvironment.GlobalIsRunningInCI ||
!TestEnvironment.IsWindows ||
AssetsJsonPath == null)
{
return;
}

lock (s_syncLock)
{
if (s_ranTestProxyValidation)
{
return;
}

s_ranTestProxyValidation = true;

try
{
if (IsTestProxyToolInstalled())
{
return;
}

string path = Path.Combine(
TestEnvironment.RepositoryRoot,
"eng",
"scripts",
"Install-TestProxyTool.ps1");

var processInfo = new ProcessStartInfo("pwsh.exe", path)
{
UseShellExecute = true
};

var process = Process.Start(processInfo);

if (process != null)
{
process.WaitForExit();
}
}
catch (Exception)
{
// Ignore
}
}
}

private bool IsTestProxyToolInstalled()
{
var processInfo = new ProcessStartInfo("dotnet.exe", "tool list --global")
{
RedirectStandardOutput = true,
UseShellExecute = false
};

var process = Process.Start(processInfo);
var output = process.StandardOutput.ReadToEnd();

if (process != null)
{
process.WaitForExit();
}

return output != null && output.Contains("azure.sdk.tools.testproxy");
}

protected TestRetryHelper TestRetryHelper => new TestRetryHelper(Mode == RecordedTestMode.Playback);
}
}
5 changes: 3 additions & 2 deletions sdk/core/Azure.Core.TestFramework/src/TestEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public abstract class TestEnvironment

private static readonly HashSet<Type> s_bootstrappingAttemptedTypes = new();
private static readonly object s_syncLock = new();
private static readonly bool s_isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
private Exception _bootstrappingException;
private readonly Type _type;
private readonly ClientDiagnostics _clientDiagnostics;
Expand Down Expand Up @@ -565,6 +564,8 @@ internal static string GetSourcePath(Assembly assembly)
return testProject;
}

public static bool IsWindows => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

/// <summary>
/// Determines if the current environment is Azure DevOps.
/// </summary>
Expand Down Expand Up @@ -655,7 +656,7 @@ private void BootStrapTestResources()
{
try
{
if (!s_isWindows ||
if (!IsWindows ||
s_bootstrappingAttemptedTypes.Contains(_type) ||
Mode == RecordedTestMode.Playback ||
GlobalIsRunningInCI)
Expand Down
4 changes: 0 additions & 4 deletions sdk/core/Azure.Core.TestFramework/src/TestRecording.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core.Pipeline;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,25 @@ public TestRecordingMismatchException()
{
}

public TestRecordingMismatchException(string message) : base(message)
public TestRecordingMismatchException(string message) : base(AppendReminder(message))
{
}

public TestRecordingMismatchException(string message, Exception innerException) : base(message, innerException)
public TestRecordingMismatchException(string message, Exception innerException) : base(AppendReminder(message), innerException)
{
}

protected TestRecordingMismatchException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}

private static string AppendReminder(string message)
{
const string Reminder = "If this is a new recording, make sure you have pushed the recordings to the assets repository.";

return TestEnvironment.GlobalIsRunningInCI
? Reminder + Environment.NewLine + Environment.NewLine + message
: message;
}
}
}

0 comments on commit 29de8a2

Please sign in to comment.