Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
451 changes: 446 additions & 5 deletions docs/decisions/azdo-test-plan-playground.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ internal static class AzureDevOpsCommandLineOptions
public const string AzureDevOpsUploadArtifactsModeOff = "off";
public const string AzureDevOpsUploadArtifactsModeTagsOnly = "tags-only";
public const string PublishAzureDevOpsRunNameOptionName = "publish-azdo-run-name";
public const string PublishAzureDevOpsTestConfigurationOptionName = "publish-azdo-test-configuration";
public const string PublishAzureDevOpsTestPlanOptionName = "publish-azdo-test-plan";
public const string PublishAzureDevOpsTestResultsOptionName = "publish-azdo-test-results";
public const string PublishAzureDevOpsTestSuiteOptionName = "publish-azdo-test-suite";
public const string RetryFailedTestsOptionName = "retry-failed-tests";

public const string OptionOn = "on";
public const string OptionOff = "off";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ public AzureDevOpsCommandLineProvider()
new CommandLineOption(AzureDevOpsCommandLineOptions.AzureDevOpsUploadArtifactName, AzureDevOpsResources.UploadArtifactNameOptionDescription, ArgumentArity.ExactlyOne, false),
new CommandLineOption(AzureDevOpsCommandLineOptions.AzureDevOpsUploadArtifacts, AzureDevOpsResources.UploadArtifactsOptionDescription, ArgumentArity.ExactlyOne, false),
new CommandLineOption(AzureDevOpsCommandLineOptions.PublishAzureDevOpsRunNameOptionName, AzureDevOpsResources.PublishAzdoRunNameOptionDescription, ArgumentArity.ExactlyOne, false),
new CommandLineOption(AzureDevOpsCommandLineOptions.PublishAzureDevOpsTestConfigurationOptionName, AzureDevOpsResources.PublishAzdoTestConfigurationOptionDescription, ArgumentArity.ExactlyOne, false),
new CommandLineOption(AzureDevOpsCommandLineOptions.PublishAzureDevOpsTestPlanOptionName, AzureDevOpsResources.PublishAzdoTestPlanOptionDescription, ArgumentArity.ExactlyOne, false),
new CommandLineOption(AzureDevOpsCommandLineOptions.PublishAzureDevOpsTestResultsOptionName, AzureDevOpsResources.PublishAzdoTestResultsOptionDescription, ArgumentArity.Zero, false),
new CommandLineOption(AzureDevOpsCommandLineOptions.PublishAzureDevOpsTestSuiteOptionName, AzureDevOpsResources.PublishAzdoTestSuiteOptionDescription, ArgumentArity.OneOrMore, false),
])
{
}
Expand All @@ -100,6 +103,10 @@ AzureDevOpsCommandLineOptions.AzureDevOpsAnnotations or AzureDevOpsCommandLineOp
=> ValidationResult.InvalidTask(string.Format(CultureInfo.InvariantCulture, AzureDevOpsResources.InvalidArtifactUploadMode, arguments[0])),
AzureDevOpsCommandLineOptions.AzureDevOpsUploadArtifactInclude or AzureDevOpsCommandLineOptions.AzureDevOpsUploadArtifactExclude
=> ValidateGlobPatternsAsync(arguments),
AzureDevOpsCommandLineOptions.PublishAzureDevOpsTestPlanOptionName
or AzureDevOpsCommandLineOptions.PublishAzureDevOpsTestSuiteOptionName
or AzureDevOpsCommandLineOptions.PublishAzureDevOpsTestConfigurationOptionName
=> ValidatePositiveIdentifiersAsync(commandOption.Name, arguments),
_ => ValidationResult.ValidTask,
};

Expand Down Expand Up @@ -179,11 +186,54 @@ public override Task<ValidationResult> ValidateCommandLineOptionsAsync(ICommandL
errorMessage = AzureDevOpsResources.PublishAzdoRunNameRequiresPublishAzdoTestResults;
}

errorMessage ??= ValidateTestPlanOptions(commandLineOptions);

return errorMessage is null
? ValidationResult.ValidTask
: ValidationResult.InvalidTask(errorMessage);
}

private static string? ValidateTestPlanOptions(ICommandLineOptions commandLineOptions)
{
bool isPlanSet = commandLineOptions.IsOptionSet(AzureDevOpsCommandLineOptions.PublishAzureDevOpsTestPlanOptionName);
if (!isPlanSet)
{
return commandLineOptions.IsOptionSet(AzureDevOpsCommandLineOptions.PublishAzureDevOpsTestSuiteOptionName)
|| commandLineOptions.IsOptionSet(AzureDevOpsCommandLineOptions.PublishAzureDevOpsTestConfigurationOptionName)
? AzureDevOpsResources.PublishAzdoTestPlanSubOptionsRequireTestPlan
: null;
}

if (!commandLineOptions.IsOptionSet(AzureDevOpsCommandLineOptions.PublishAzureDevOpsTestResultsOptionName))
{
return AzureDevOpsResources.PublishAzdoTestPlanRequiresPublishAzdoTestResults;
}

if (!commandLineOptions.IsOptionSet(AzureDevOpsCommandLineOptions.PublishAzureDevOpsTestConfigurationOptionName))
{
return AzureDevOpsResources.PublishAzdoTestPlanRequiresConfiguration;
}

// The Retry extension is not referenced by this assembly, so its option is matched by name.
return commandLineOptions.IsOptionSet(AzureDevOpsCommandLineOptions.RetryFailedTestsOptionName)
? AzureDevOpsResources.PublishAzdoTestPlanConflictsWithRetry
: null;
}

private static Task<ValidationResult> ValidatePositiveIdentifiersAsync(string optionName, string[] arguments)
{
HashSet<int> identifiers = [];
foreach (string argument in arguments)
{
if (!AzureDevOpsTestPlanSelectionOptions.TryParseIdentifier(argument, out int identifier) || !identifiers.Add(identifier))
{
return ValidationResult.InvalidTask(string.Format(CultureInfo.InvariantCulture, AzureDevOpsResources.InvalidAzureDevOpsIdentifier, argument, optionName));
}
}

return ValidationResult.ValidTask;
}

private static bool HasArtifactUploadConfiguration(ICommandLineOptions commandLineOptions)
=> commandLineOptions.IsOptionSet(AzureDevOpsCommandLineOptions.AzureDevOpsUploadArtifactInclude)
|| commandLineOptions.IsOptionSet(AzureDevOpsCommandLineOptions.AzureDevOpsUploadArtifactExclude)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Testing.Platform.Builder;
using Microsoft.Testing.Platform.Extensions;
using Microsoft.Testing.Platform.Extensions.ArtifactPostProcessing;
using Microsoft.Testing.Platform.Helpers;
using Microsoft.Testing.Platform.Logging;
using Microsoft.Testing.Platform.ServerMode;
using Microsoft.Testing.Platform.Services;
Expand All @@ -27,6 +28,30 @@ public static void AddAzureDevOpsProvider(this ITestApplicationBuilder builder)
{
AzureDevOpsHistoryService? historyService = null;

// The filter provider and the plan publisher must observe the same resolved plan.
AzureDevOpsTestPlanSelection? testPlanSelection = null;
AzureDevOpsTestPlanSelection GetTestPlanSelection(IServiceProvider serviceProvider)
=> testPlanSelection ??= new AzureDevOpsTestPlanSelection(
serviceProvider.GetCommandLineOptions(),
serviceProvider.GetConfiguration(),
serviceProvider.GetEnvironment(),
serviceProvider.GetTestApplicationModuleInfo(),
new AzureDevOpsTestResultsClient(serviceProvider.GetTask(), serviceProvider.GetClock(), serviceProvider.GetLoggerFactory()));

var compositeTestPlanPublisher =
new CompositeExtensionFactory<AzureDevOpsTestPlanPublisher>(serviceProvider =>
new AzureDevOpsTestPlanPublisher(
GetTestPlanSelection(serviceProvider),
new AzureDevOpsTestResultsClient(serviceProvider.GetTask(), serviceProvider.GetClock(), serviceProvider.GetLoggerFactory()),
serviceProvider.GetOutputDevice(),
serviceProvider.GetTestApplicationProcessExitCode(),
serviceProvider.GetLoggerFactory(),
serviceProvider.GetClock()));

#pragma warning disable TPEXP // Test execution filter providers are experimental.
builder.AddTestExecutionFilterProvider(serviceProvider =>
new AzureDevOpsTestPlanFilterProvider(GetTestPlanSelection(serviceProvider), serviceProvider.GetOutputDevice(), serviceProvider.GetTestApplicationProcessExitCode()));
#pragma warning restore TPEXP
var compositeArtifactUploader =
new CompositeExtensionFactory<AzureDevOpsArtifactUploader>(serviceProvider =>
new AzureDevOpsArtifactUploader(
Expand Down Expand Up @@ -106,13 +131,15 @@ public static void AddAzureDevOpsProvider(this ITestApplicationBuilder builder)
builder.TestHost.AddDataConsumer(compositeSummaryReporter);
builder.TestHost.AddDataConsumer(compositeSlowTestReporter);
builder.TestHost.AddDataConsumer(compositeTestResultsPublisher);
builder.TestHost.AddDataConsumer(compositeTestPlanPublisher);
builder.TestHost.AddDataConsumer(compositeLogGroupReporter);
builder.TestHost.AddTestSessionLifetimeHandler(serviceProvider =>
historyService ??= CreateHistoryService(serviceProvider));
builder.TestHost.AddTestSessionLifetimeHandler(compositeArtifactUploader);
builder.TestHost.AddTestSessionLifetimeHandler(compositeSummaryReporter);
builder.TestHost.AddTestSessionLifetimeHandler(compositeSlowTestReporter);
builder.TestHost.AddTestSessionLifetimeHandler(compositeTestResultsPublisher);
builder.TestHost.AddTestSessionLifetimeHandler(compositeTestPlanPublisher);

// Registered last so its OnTestSessionFinishingAsync (the closing ##[endgroup]) runs after
// the other AzDO handlers' finishing callbacks, ensuring the group wraps all their output.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Testing.Extensions.AzureDevOpsReport.Resources;
using Microsoft.Testing.Platform.Extensions.Messages;
using Microsoft.Testing.Platform.Extensions.OutputDevice;
using Microsoft.Testing.Platform.OutputDevice;
using Microsoft.Testing.Platform.Requests;
using Microsoft.Testing.Platform.Services;

namespace Microsoft.Testing.Extensions.AzureDevOpsReport;

#pragma warning disable TPEXP // Test execution filter providers are experimental.

/// <summary>
/// Restricts the run to the automated tests associated with the requested Azure Test Plan.
/// </summary>
/// <remarks>
/// Tests are selected by their canonical name, which is the value Azure DevOps stores in the test case's
/// <c>AutomatedTestName</c> field. The selection fails closed: when the plan cannot be resolved the run
/// fails instead of silently executing every test.
/// </remarks>
internal sealed class AzureDevOpsTestPlanFilterProvider : ITestExecutionFilterProvider, IOutputDeviceDataProducer
{
private readonly AzureDevOpsTestPlanSelection _selection;
private readonly IOutputDevice _outputDevice;
private readonly ITestApplicationProcessExitCode _testApplicationProcessExitCode;

public AzureDevOpsTestPlanFilterProvider(AzureDevOpsTestPlanSelection selection, IOutputDevice outputDevice, ITestApplicationProcessExitCode testApplicationProcessExitCode)
{
_selection = selection;
_outputDevice = outputDevice;
_testApplicationProcessExitCode = testApplicationProcessExitCode;
}

public string Uid => nameof(AzureDevOpsTestPlanFilterProvider);

public string Version => ExtensionVersion.DefaultSemVer;

public string DisplayName => AzureDevOpsResources.DisplayName;

public string Description => AzureDevOpsResources.Description;

public Task<bool> IsEnabledAsync()
=> Task.FromResult(_selection.Options is not null);

public async Task<ITestExecutionFilter?> GetFilterAsync(TestExecutionFilterContext context, CancellationToken cancellationToken)
{
if (_selection.Options is not { } options)
{
return null;
}

// Server-mode clients (IDEs) send their own selection, which the plan must not silently override.
if (context.Origin == TestExecutionRequestOrigin.Server)
{
throw new InvalidOperationException(AzureDevOpsResources.AzureDevOpsTestPlanServerModeNotSupported);
}

AzureDevOpsResolvedTestPlan plan;
try
{
plan = await _selection.ResolveAsync(context.RequestKind, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex) when (ex is InvalidOperationException or HttpRequestException or IOException or FormatException
|| (ex is OperationCanceledException && !cancellationToken.IsCancellationRequested))
{
await _testApplicationProcessExitCode.SetTestAdapterTestSessionFailureAsync(
string.Format(CultureInfo.InvariantCulture, AzureDevOpsResources.AzureDevOpsTestPlanSelectionFailed, options.PlanId, ex.Message),
cancellationToken).ConfigureAwait(false);
return new TestNodeUidListFilter([]);
}

await _outputDevice.DisplayAsync(
this,
new SessionMessageOutputDeviceData(string.Format(CultureInfo.InvariantCulture, AzureDevOpsResources.AzureDevOpsTestPlanSelected, plan.PlanId, plan.TestNames.Count, plan.Points.Count)),
cancellationToken).ConfigureAwait(false);

return new TestNodeUidListFilter([.. plan.TestNames.Select(static name => new TestNodeUid(name))]);
}
}
Loading