forked from microsoft/mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestRunGetCommand.cs
More file actions
127 lines (116 loc) · 5.47 KB
/
TestRunGetCommand.cs
File metadata and controls
127 lines (116 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Azure.Mcp.Tools.LoadTesting.Models.LoadTestRun;
using Azure.Mcp.Tools.LoadTesting.Options;
using Azure.Mcp.Tools.LoadTesting.Options.LoadTestRun;
using Azure.Mcp.Tools.LoadTesting.Services;
using Microsoft.Extensions.Logging;
using Microsoft.Mcp.Core.Commands;
using Microsoft.Mcp.Core.Extensions;
using Microsoft.Mcp.Core.Models.Command;
using Microsoft.Mcp.Core.Models.Option;
namespace Azure.Mcp.Tools.LoadTesting.Commands.LoadTestRun;
public sealed class TestRunGetCommand(ILogger<TestRunGetCommand> logger, ILoadTestingService loadTestingService)
: BaseLoadTestingCommand<TestRunGetOptions>
{
private const string _commandTitle = "Test Run Get";
private readonly ILogger<TestRunGetCommand> _logger = logger;
private readonly ILoadTestingService _loadTestingService = loadTestingService;
public override string Id => "713313ec-b9a5-4a71-9953-5b2d4a7b5d7b";
public override string Name => "get";
public override string Description =>
$"""
Get load test run details by testrun ID, or list all test runs by test ID.
Returns execution details including status, start/end times, progress, metrics, and artifacts.
Does not return test configuration or resource details.
""";
public override string Title => _commandTitle;
public override ToolMetadata Metadata => new()
{
Destructive = false,
Idempotent = true,
OpenWorld = false,
ReadOnly = true,
LocalRequired = false,
Secret = false
};
protected override void RegisterOptions(Command command)
{
base.RegisterOptions(command);
command.Options.Add(LoadTestingOptionDefinitions.TestResource.AsRequired());
command.Options.Add(OptionDefinitions.Common.ResourceGroup.AsOptional());
command.Options.Add(LoadTestingOptionDefinitions.TestRun.AsOptional());
command.Options.Add(LoadTestingOptionDefinitions.Test.AsOptional());
command.Validators.Add(commandResult =>
{
var testRunId = commandResult.GetValueWithoutDefault(LoadTestingOptionDefinitions.TestRun);
var testId = commandResult.GetValueWithoutDefault(LoadTestingOptionDefinitions.Test);
if (string.IsNullOrEmpty(testRunId) && string.IsNullOrEmpty(testId))
{
commandResult.AddError("Either --testrun or --test must be provided.");
commandResult.AddError("Either --testrun or --test must be provided. Pass --testrun to get details about a specific run or pass --test to list all test runs for the test.");
}
else if (!string.IsNullOrEmpty(testRunId) && !string.IsNullOrEmpty(testId))
{
commandResult.AddError("Cannot specify both --testrun and --test. Use one or the other.");
}
});
}
protected override TestRunGetOptions BindOptions(ParseResult parseResult)
{
var options = base.BindOptions(parseResult);
options.TestRunId = parseResult.GetValueOrDefault<string>(LoadTestingOptionDefinitions.TestRun.Name);
options.TestId = parseResult.GetValueOrDefault<string>(LoadTestingOptionDefinitions.Test.Name);
return options;
}
public override async Task<CommandResponse> ExecuteAsync(CommandContext context, ParseResult parseResult, CancellationToken cancellationToken)
{
if (!Validate(parseResult.CommandResult, context.Response).IsValid)
{
return context.Response;
}
var options = BindOptions(parseResult);
try
{
// If TestRunId is provided, get a single test run
if (!string.IsNullOrEmpty(options.TestRunId))
{
var result = await _loadTestingService.GetLoadTestRunAsync(
options.Subscription!,
options.TestResourceName!,
options.TestRunId!,
options.ResourceGroup,
options.Tenant,
options.RetryPolicy,
cancellationToken);
// Set results if any were returned
context.Response.Results = result != null
? ResponseResult.Create(new([result]), LoadTestJsonContext.Default.TestRunGetCommandResult)
: null;
}
// Otherwise if TestId is provided, list all test runs for that test
else if (!string.IsNullOrEmpty(options.TestId))
{
var results = await _loadTestingService.GetLoadTestRunsFromTestIdAsync(
options.Subscription!,
options.TestResourceName!,
options.TestId!,
options.ResourceGroup,
options.Tenant,
options.RetryPolicy,
cancellationToken);
context.Response.Results = ResponseResult.Create(new(results ?? []), LoadTestJsonContext.Default.TestRunGetCommandResult);
}
// If neither is provided, that's ok - validation will catch it
}
catch (Exception ex)
{
// Log error with context information
_logger.LogError(ex, "Error in {Operation}. Options: {Options}", Name, options);
// Let base class handle standard error processing
HandleException(context, ex);
}
return context.Response;
}
internal record TestRunGetCommandResult(List<TestRun> TestRuns);
}