-
Notifications
You must be signed in to change notification settings - Fork 436
Expand file tree
/
Copy pathTestCreateCommand.cs
More file actions
114 lines (102 loc) · 5.22 KB
/
TestCreateCommand.cs
File metadata and controls
114 lines (102 loc) · 5.22 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Azure.Mcp.Core.Extensions;
using Azure.Mcp.Core.Models.Option;
using Azure.Mcp.Tools.LoadTesting.Models.LoadTest;
using Azure.Mcp.Tools.LoadTesting.Options;
using Azure.Mcp.Tools.LoadTesting.Options.LoadTest;
using Azure.Mcp.Tools.LoadTesting.Services;
using Microsoft.Extensions.Logging;
using Microsoft.Mcp.Core.Commands;
using Microsoft.Mcp.Core.Models.Command;
using Microsoft.Mcp.Core.Models.Option;
namespace Azure.Mcp.Tools.LoadTesting.Commands.LoadTest;
public sealed class TestCreateCommand(ILogger<TestCreateCommand> logger)
: BaseLoadTestingCommand<TestCreateOptions>(resourceGroupRequired: false, testResourceRequired: true)
{
private const string _commandTitle = "Test Create";
private readonly ILogger<TestCreateCommand> _logger = logger;
public override string Id => "2153384b-02ea-47b3-a069-7f5f9a709d66";
public override string Name => "create";
public override string Description =>
$"""
Creates a new load test plan or configuration for performance testing scenarios. This command creates a basic URL-based load test that can be used to evaluate the performance
and scalability of web applications and APIs. The test configuration defines target endpoint, load parameters, and test duration. Once we create a test plan, we can use that to trigger test runs to test the endpoints set using the 'azmcp loadtesting testrun create' command.
This is NOT going to trigger or create any test runs and only will setup your test plan. Also, this is NOT going to create any test resource in azure.
It will only create a test in an already existing load test resource.
""";
public override string Title => _commandTitle;
public override ToolMetadata Metadata => new()
{
Destructive = true,
Idempotent = false,
OpenWorld = false,
ReadOnly = false,
LocalRequired = false,
Secret = false
};
protected override void RegisterOptions(Command command)
{
base.RegisterOptions(command);
command.Options.Add(LoadTestingOptionDefinitions.Test.AsRequired());
command.Options.Add(LoadTestingOptionDefinitions.Description.AsOptional());
command.Options.Add(LoadTestingOptionDefinitions.DisplayName.AsOptional());
command.Options.Add(LoadTestingOptionDefinitions.Endpoint.AsOptional());
command.Options.Add(LoadTestingOptionDefinitions.VirtualUsers.AsOptional());
command.Options.Add(LoadTestingOptionDefinitions.Duration.AsOptional());
command.Options.Add(LoadTestingOptionDefinitions.RampUpTime.AsOptional());
}
protected override TestCreateOptions BindOptions(ParseResult parseResult)
{
var options = base.BindOptions(parseResult);
options.TestId = parseResult.GetValueOrDefault<string>(LoadTestingOptionDefinitions.Test.Name);
options.Description = parseResult.GetValueOrDefault<string>(LoadTestingOptionDefinitions.Description.Name);
options.DisplayName = parseResult.GetValueOrDefault<string>(LoadTestingOptionDefinitions.DisplayName.Name);
options.Endpoint = parseResult.GetValueOrDefault<string>(LoadTestingOptionDefinitions.Endpoint.Name);
options.VirtualUsers = parseResult.GetValueOrDefault<int>(LoadTestingOptionDefinitions.VirtualUsers.Name);
options.Duration = parseResult.GetValueOrDefault<int>(LoadTestingOptionDefinitions.Duration.Name);
options.RampUpTime = parseResult.GetValueOrDefault<int>(LoadTestingOptionDefinitions.RampUpTime.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
{
// Get the appropriate service from DI
var service = context.GetService<ILoadTestingService>();
// Call service operation(s)
var results = await service.CreateTestAsync(
options.Subscription!,
options.TestResourceName!,
options.TestId!,
options.ResourceGroup,
options.DisplayName,
options.Description,
options.Duration,
options.VirtualUsers,
options.RampUpTime,
options.Endpoint,
options.Tenant,
options.RetryPolicy,
cancellationToken);
// Set results if any were returned
context.Response.Results = results != null ?
ResponseResult.Create(new(results), LoadTestJsonContext.Default.TestCreateCommandResult) :
null;
}
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 TestCreateCommandResult(Test Test);
}