Skip to content

Commit 599a7b9

Browse files
Fixed + improved ImdsTests
1 parent e62cc4d commit 599a7b9

File tree

3 files changed

+19
-26
lines changed

3 files changed

+19
-26
lines changed

tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ImdsTests.cs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ public async Task ImdsFails404TwiceThenSucceeds200Async(
3939
.WithHttpManager(httpManager)
4040
.WithRetryPolicyFactory(_testRetryPolicyFactory);
4141

42-
// Disable cache to avoid pollution
43-
44-
4542
IManagedIdentityApplication mi = miBuilder.Build();
4643

44+
ManagedIdentityTests.MockImdsV1Probe(httpManager, ManagedIdentitySource.Imds, userAssignedIdentityId, userAssignedId);
45+
4746
// Simulate two 404s (to trigger retries), then a successful response
4847
const int Num404Errors = 2;
4948
for (int i = 0; i < Num404Errors; i++)
@@ -98,11 +97,10 @@ public async Task ImdsFails410FourTimesThenSucceeds200Async(
9897
.WithHttpManager(httpManager)
9998
.WithRetryPolicyFactory(_testRetryPolicyFactory);
10099

101-
// Disable cache to avoid pollution
102-
103-
104100
IManagedIdentityApplication mi = miBuilder.Build();
105101

102+
ManagedIdentityTests.MockImdsV1Probe(httpManager, ManagedIdentitySource.Imds, userAssignedIdentityId, userAssignedId);
103+
106104
// Simulate four 410s (to trigger retries), then a successful response
107105
const int Num410Errors = 4;
108106
for (int i = 0; i < Num410Errors; i++)
@@ -157,11 +155,10 @@ public async Task ImdsFails410PermanentlyAsync(
157155
.WithHttpManager(httpManager)
158156
.WithRetryPolicyFactory(_testRetryPolicyFactory);
159157

160-
// Disable cache to avoid pollution
161-
162-
163158
IManagedIdentityApplication mi = miBuilder.Build();
164159

160+
ManagedIdentityTests.MockImdsV1Probe(httpManager, ManagedIdentitySource.Imds, userAssignedIdentityId, userAssignedId);
161+
165162
// Simulate permanent 410s (to trigger the maximum number of retries)
166163
const int Num410Errors = 1 + TestImdsRetryPolicy.LinearStrategyNumRetries; // initial request + maximum number of retries
167164
for (int i = 0; i < Num410Errors; i++)
@@ -213,11 +210,10 @@ public async Task ImdsFails504PermanentlyAsync(
213210
.WithHttpManager(httpManager)
214211
.WithRetryPolicyFactory(_testRetryPolicyFactory);
215212

216-
// Disable cache to avoid pollution
217-
218-
219213
IManagedIdentityApplication mi = miBuilder.Build();
220214

215+
ManagedIdentityTests.MockImdsV1Probe(httpManager, ManagedIdentitySource.Imds, userAssignedIdentityId, userAssignedId);
216+
221217
// Simulate permanent 504s (to trigger the maximum number of retries)
222218
const int Num504Errors = 1 + TestImdsRetryPolicy.ExponentialStrategyNumRetries; // initial request + maximum number of retries
223219
for (int i = 0; i < Num504Errors; i++)
@@ -269,11 +265,10 @@ public async Task ImdsFails400WhichIsNonRetriableAndRetryPolicyIsNotTriggeredAsy
269265
.WithHttpManager(httpManager)
270266
.WithRetryPolicyFactory(_testRetryPolicyFactory);
271267

272-
// Disable cache to avoid pollution
273-
274-
275268
IManagedIdentityApplication mi = miBuilder.Build();
276269

270+
ManagedIdentityTests.MockImdsV1Probe(httpManager, ManagedIdentitySource.Imds, userAssignedIdentityId, userAssignedId);
271+
277272
httpManager.AddManagedIdentityMockHandler(
278273
ManagedIdentityTests.ImdsEndpoint,
279274
ManagedIdentityTests.Resource,
@@ -321,11 +316,10 @@ public async Task ImdsFails500AndRetryPolicyIsDisabledAndNotTriggeredAsync(
321316
.WithHttpManager(httpManager)
322317
.WithRetryPolicyFactory(_testRetryPolicyFactory);
323318

324-
// Disable cache to avoid pollution
325-
326-
327319
IManagedIdentityApplication mi = miBuilder.Build();
328320

321+
ManagedIdentityTests.MockImdsV1Probe(httpManager, ManagedIdentitySource.Imds, userAssignedIdentityId, userAssignedId);
322+
329323
httpManager.AddManagedIdentityMockHandler(
330324
ManagedIdentityTests.ImdsEndpoint,
331325
ManagedIdentityTests.Resource,
@@ -367,11 +361,10 @@ public async Task ImdsRetryPolicyLifeTimeIsPerRequestAsync()
367361
.WithHttpManager(httpManager)
368362
.WithRetryPolicyFactory(_testRetryPolicyFactory);
369363

370-
// Disable cache to avoid pollution
371-
372-
373364
IManagedIdentityApplication mi = miBuilder.Build();
374365

366+
ManagedIdentityTests.MockImdsV1Probe(httpManager, ManagedIdentitySource.Imds);
367+
375368
// Simulate permanent errors (to trigger the maximum number of retries)
376369
const int Num504Errors = 1 + TestImdsRetryPolicy.ExponentialStrategyNumRetries; // initial request + maximum number of retries
377370
for (int i = 0; i < Num504Errors; i++)

tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ImdsV2Tests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private async Task<IManagedIdentityApplication> CreateManagedIdentityAsync(
9090
bool addProbeMock = true,
9191
bool addSourceCheck = true,
9292
ManagedIdentityKeyType managedIdentityKeyType = ManagedIdentityKeyType.InMemory,
93-
bool imdsV2 = true) // false indicates imdsV1
93+
ImdsVersion imdsVersion = ImdsVersion.V2)
9494
{
9595
ManagedIdentityApplicationBuilder miBuilder = null;
9696

@@ -108,14 +108,14 @@ private async Task<IManagedIdentityApplication> CreateManagedIdentityAsync(
108108
.WithHttpManager(httpManager)
109109
.WithRetryPolicyFactory(_testRetryPolicyFactory);
110110

111-
if (imdsV2)
111+
if (imdsVersion == ImdsVersion.V2)
112112
{
113113
miBuilder.WithCsrFactory(_testCsrFactory);
114114
}
115115

116116
var managedIdentityApp = miBuilder.Build();
117117

118-
if (!imdsV2)
118+
if (imdsVersion == ImdsVersion.V1)
119119
{
120120
httpManager.AddMockHandler(MockHelpers.MockImdsProbeFailure(ImdsVersion.V2, userAssignedIdentityId, userAssignedId));
121121
httpManager.AddMockHandler(MockHelpers.MockImdsProbe(ImdsVersion.V1, userAssignedIdentityId, userAssignedId));
@@ -345,7 +345,7 @@ public async Task ImdsV2EndpointsAreNotAvailableButMtlsPopTokenWasRequested(
345345
{
346346
SetEnvironmentVariables(ManagedIdentitySource.Imds, TestConstants.ImdsEndpoint);
347347

348-
var managedIdentityApp = await CreateManagedIdentityAsync(httpManager, userAssignedIdentityId, userAssignedId, imdsV2: false).ConfigureAwait(false);
348+
var managedIdentityApp = await CreateManagedIdentityAsync(httpManager, userAssignedIdentityId, userAssignedId, imdsVersion: ImdsVersion.V1).ConfigureAwait(false);
349349

350350
var ex = await Assert.ThrowsExceptionAsync<MsalClientException>(async () =>
351351
await managedIdentityApp.AcquireTokenForManagedIdentity(ManagedIdentityTests.Resource)

tests/Microsoft.Identity.Test.Unit/ManagedIdentityTests/ManagedIdentityTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class ManagedIdentityTests : TestBase
4343
private readonly TestRetryPolicyFactory _testRetryPolicyFactory = new TestRetryPolicyFactory();
4444

4545
// MtlsPop is disabled for all these tests, so no need to mock IMDSv2 probe here
46-
private void MockImdsV1Probe(
46+
internal static void MockImdsV1Probe(
4747
MockHttpManager httpManager,
4848
ManagedIdentitySource managedIdentitySource,
4949
UserAssignedIdentityId userAssignedIdentityId = UserAssignedIdentityId.None,

0 commit comments

Comments
 (0)