Skip to content

Commit c6db961

Browse files
authored
Migrate Managed Lustre to recording framework (#1337)
* Partial Managed Lustre migration * Add helper method to RecordedCommandTestBase * Complete work * Remove ifdef trying to change polling, need a different design in the future * Small change * Small change
1 parent b9c7ff3 commit c6db961

File tree

5 files changed

+187
-56
lines changed

5 files changed

+187
-56
lines changed

core/Azure.Mcp.Core/tests/Azure.Mcp.Tests/Client/RecordedCommandTestsBase.cs

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ public abstract class RecordedCommandTestsBase(ITestOutputHelper output, TestPro
2929
/// <summary>
3030
/// Sanitizers that will apply generally across all parts (URI, Body, HeaderValues) of the request/response. This sanitization is applied to to recorded data at rest and during recording, and against test requests during playback.
3131
/// </summary>
32-
public virtual List<GeneralRegexSanitizer> GeneralRegexSanitizers { get; } = new();
32+
public virtual List<GeneralRegexSanitizer> GeneralRegexSanitizers { get; } = [];
3333

3434
/// <summary>
3535
/// Sanitizers that will apply a regex to specific headers. This sanitization is applied to to recorded data at rest and during recording, and against test requests during playback.
3636
/// </summary>
37-
public virtual List<HeaderRegexSanitizer> HeaderRegexSanitizers { get; } = new()
38-
{
37+
public virtual List<HeaderRegexSanitizer> HeaderRegexSanitizers { get; } =
38+
[
3939
// Sanitize the WWW-Authenticate header which may contain tenant IDs or resource URLs to "Sanitized"
4040
// During conversion to recordings, the actual tenant ID is captured in group 1 and replaced with a fixed GUID.
4141
// REMOVAL of this formatting cause complete failure on tool side when it expects a valid URL with a GUID tenant ID.
@@ -46,37 +46,37 @@ public abstract class RecordedCommandTestsBase(ITestOutputHelper output, TestPro
4646
GroupForReplace = "1",
4747
Value = EmptyGuid
4848
})
49-
};
49+
];
5050

5151
/// <summary>
5252
/// Sanitizers that apply a regex replacement to URIs. This sanitization is applied to to recorded data at rest and during recording, and against test requests during playback.
5353
/// </summary>
54-
public virtual List<UriRegexSanitizer> UriRegexSanitizers { get; } = new();
54+
public virtual List<UriRegexSanitizer> UriRegexSanitizers { get; } = [];
5555

5656
/// <summary>
5757
/// Sanitizers that will apply a regex replacement to a specific json body key. This sanitization is applied to to recorded data at rest and during recording, and against test requests during playback.
5858
/// </summary>
59-
public virtual List<BodyKeySanitizer> BodyKeySanitizers { get; } = new();
59+
public virtual List<BodyKeySanitizer> BodyKeySanitizers { get; } = [];
6060

6161
/// <summary>
6262
/// Sanitizers that will apply regex replacement to the body of requests/responses. This sanitization is applied to to recorded data at rest and during recording, and against test requests during playback.
6363
/// </summary>
64-
public virtual List<BodyRegexSanitizer> BodyRegexSanitizers { get; } = new();
64+
public virtual List<BodyRegexSanitizer> BodyRegexSanitizers { get; } = [];
6565

6666
/// <summary>
6767
/// The test-proxy has a default set of ~90 sanitizers for common sensitive data (GUIDs, tokens, timestamps, etc). This list allows opting out of specific default sanitizers by name.
6868
/// Grab the names from the test-proxy source at https://github.com/Azure/azure-sdk-tools/blob/main/tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/SanitizerDictionary.cs#L65)
6969
/// Default Set:
7070
/// - `AZSDK3430`: `$..id`
7171
/// </summary>
72-
public virtual List<string> DisabledDefaultSanitizers { get; } = new() { "AZSDK3430" };
72+
public virtual List<string> DisabledDefaultSanitizers { get; } = ["AZSDK3430"];
7373

7474
/// <summary>
7575
/// During recording, variables saved to this dictionary will be propagated to the test-proxy and saved in the recording file.
7676
/// During playback, these variables will be available within the test function body, and can be used to ensure that dynamic values from the recording are used where
7777
/// specific values should be used.
7878
/// </summary>
79-
protected readonly Dictionary<string, string> TestVariables = new Dictionary<string, string>();
79+
protected readonly Dictionary<string, string> TestVariables = [];
8080

8181
/// <summary>
8282
/// When set, applies a custom matcher for _all_ playback tests from this test class. This can be overridden on a per-test basis using the <see cref="CustomMatcherAttribute"/> attribute on test methods.
@@ -94,6 +94,31 @@ public virtual void RegisterVariable(string name, string value)
9494
TestVariables[name] = value;
9595
}
9696

97+
/// <summary>
98+
/// Registers a variable to or retrieves it from the session record. This is a convenience equivalent to calling
99+
/// <see cref="RegisterVariable(string, string)"/> and then retrieving the value from <see cref="TestVariables"/>.
100+
/// If the test mode is playback, it will load attempt to load the variable and return it. If the test mode is
101+
/// record, it will store the value and return it.
102+
/// </summary>
103+
/// <param name="name">The name of the variable to register or retrieve.</param>
104+
/// <param name="value">The value reference to register or retrieve.</param>
105+
/// <returns>The value of the variable.</returns>
106+
public virtual string RegisterOrRetrieveVariable(string name, string value)
107+
{
108+
if (TestMode == TestMode.Record)
109+
{
110+
// store the value in the recording
111+
TestVariables[name] = value;
112+
}
113+
else if (TestMode == TestMode.Playback)
114+
{
115+
// retrieve the value from the recording
116+
value = TestVariables[name];
117+
}
118+
119+
return value;
120+
}
121+
97122
// used to resolve a recording "path" given an invoking test
98123
protected static readonly RecordingPathResolver PathResolver = new();
99124

@@ -238,7 +263,7 @@ private async Task DisableSanitizersAsync()
238263
{
239264
if (DisabledDefaultSanitizers.Count > 0)
240265
{
241-
var toRemove = new SanitizerList(new List<string>());
266+
var toRemove = new SanitizerList([]);
242267
foreach (var sanitizer in DisabledDefaultSanitizers)
243268
{
244269
toRemove.Sanitizers.Add(sanitizer);

tools/Azure.Mcp.Tools.AppConfig/tests/Azure.Mcp.Tools.AppConfig.LiveTests/AppConfigCommandTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class AppConfigCommandTests : RecordedCommandTestsBase
3131
/// AZSDK3493 = $..name
3232
/// AZSDK3447 = $..key
3333
/// </summary>
34-
public override List<string> DisabledDefaultSanitizers => base.DisabledDefaultSanitizers.Concat(new[] { "AZSDK3493", "AZSDK3447" }).ToList();
34+
public override List<string> DisabledDefaultSanitizers => [.. base.DisabledDefaultSanitizers, "AZSDK3493", "AZSDK3447"];
3535

3636
public AppConfigCommandTests(ITestOutputHelper output, TestProxyFixture fixture) : base(output, fixture)
3737
{

tools/Azure.Mcp.Tools.KeyVault/tests/Azure.Mcp.Tools.KeyVault.LiveTests/KeyVaultCommandTests.cs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@ public class KeyVaultCommandTests(ITestOutputHelper output, TestProxyFixture fix
1818
{
1919
private readonly KeyVaultTestCertificateAssets _importCertificateAssets = KeyVaultTestCertificates.Load();
2020

21-
public override List<BodyRegexSanitizer> BodyRegexSanitizers => new List<BodyRegexSanitizer>() {
21+
public override List<BodyRegexSanitizer> BodyRegexSanitizers => [
2222
// Sanitizes all hostnames in URLs to remove actual vault names (not limited to `kid` fields)
2323
new BodyRegexSanitizer(new BodyRegexSanitizerBody() {
2424
Regex = "(?<=http://|https://)(?<host>[^/?\\.]+)",
2525
GroupForReplace = "host",
2626
})
27-
};
27+
];
2828

2929
public override List<BodyKeySanitizer> BodyKeySanitizers
3030
{
3131
get
3232
{
33-
return new List<BodyKeySanitizer>()
34-
{
33+
return
34+
[
3535
new BodyKeySanitizer(new BodyKeySanitizerBody("value")
3636
{
3737
Value = _importCertificateAssets.PfxBase64
@@ -44,7 +44,7 @@ public override List<BodyKeySanitizer> BodyKeySanitizers
4444
{
4545
Value = _importCertificateAssets.CsrBase64
4646
})
47-
};
47+
];
4848
}
4949
}
5050

@@ -90,23 +90,21 @@ public async Task Should_get_key()
9090
[Fact]
9191
public async Task Should_create_key()
9292
{
93-
var keyName = "key" + Random.Shared.NextInt64();
94-
95-
RegisterVariable("keyName", keyName);
93+
var keyName = RegisterOrRetrieveVariable("keyName", "key" + Random.Shared.NextInt64());
9694

9795
var result = await CallToolAsync(
9896
"keyvault_key_create",
9997
new()
10098
{
10199
{ "subscription", Settings.SubscriptionId },
102100
{ "vault", Settings.ResourceBaseName },
103-
{ "key", TestVariables["keyName"]},
101+
{ "key", keyName},
104102
{ "key-type", KeyType.Rsa.ToString() }
105103
});
106104

107105
var createdKeyName = result.AssertProperty("name");
108106
Assert.Equal(JsonValueKind.String, createdKeyName.ValueKind);
109-
Assert.Equal(TestVariables["keyName"], createdKeyName.GetString());
107+
Assert.Equal(keyName, createdKeyName.GetString());
110108

111109
var keyType = result.AssertProperty("keyType");
112110
Assert.Equal(JsonValueKind.String, keyType.ValueKind);
@@ -193,22 +191,20 @@ public async Task Should_get_certificate()
193191
[Fact]
194192
public async Task Should_create_certificate()
195193
{
196-
var certificateName = "certificate" + Random.Shared.NextInt64();
197-
198-
RegisterVariable("certificateName", certificateName);
194+
var certificateName = RegisterOrRetrieveVariable("certificateName", "certificate" + Random.Shared.NextInt64());
199195

200196
var result = await CallToolAsync(
201197
"keyvault_certificate_create",
202198
new()
203199
{
204200
{ "subscription", Settings.SubscriptionId },
205201
{ "vault", Settings.ResourceBaseName },
206-
{ "certificate", TestVariables["certificateName"]}
202+
{ "certificate", certificateName}
207203
});
208204

209205
var createdCertificateName = result.AssertProperty("name");
210206
Assert.Equal(JsonValueKind.String, createdCertificateName.ValueKind);
211-
Assert.Equal(TestVariables["certificateName"], createdCertificateName.GetString());
207+
Assert.Equal(certificateName, createdCertificateName.GetString());
212208

213209
// Verify that the certificate has some expected properties
214210
ValidateCertificate(result);
@@ -224,23 +220,21 @@ public async Task Should_import_certificate()
224220

225221
try
226222
{
227-
var certificateName = "certificateimport" + Random.Shared.NextInt64();
228-
229-
RegisterVariable("certificateName", certificateName);
223+
var certificateName = RegisterOrRetrieveVariable("certificateName", "certificateimport" + Random.Shared.NextInt64());
230224

231225
var result = await CallToolAsync(
232226
"keyvault_certificate_import",
233227
new()
234228
{
235229
{ "subscription", Settings.SubscriptionId },
236230
{ "vault", Settings.ResourceBaseName },
237-
{ "certificate", TestVariables["certificateName"] },
231+
{ "certificate", certificateName },
238232
{ "certificate-data", tempPath },
239233
{ "password", fakePassword }
240234
});
241235
var createdCertificateName = result.AssertProperty("name");
242236
Assert.Equal(JsonValueKind.String, createdCertificateName.ValueKind);
243-
Assert.Equal(TestVariables["certificateName"], createdCertificateName.GetString());
237+
Assert.Equal(certificateName, createdCertificateName.GetString());
244238
// Validate basic certificate properties
245239
ValidateCertificate(result);
246240
}

0 commit comments

Comments
 (0)