Skip to content

Commit ee1ba8f

Browse files
authored
Refactor TestEnvVar (Azure#18364)
Improve `TestEnvVar` to handle consistent cleaning and stashing of existing environment variables.
1 parent 797a12c commit ee1ba8f

File tree

6 files changed

+134
-123
lines changed

6 files changed

+134
-123
lines changed

sdk/identity/Azure.Identity/tests/DefaultAzureCredentialTests.cs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void ValidateCtorNoOptions()
3838
}
3939

4040
[Test]
41-
public void ValidateCtorIncludedInteractiveParam([Values(true, false)]bool includeInteractive)
41+
public void ValidateCtorIncludedInteractiveParam([Values(true, false)] bool includeInteractive)
4242
{
4343
var cred = new DefaultAzureCredential(includeInteractive);
4444

@@ -110,7 +110,7 @@ public void ValidateCtorOptionsPassedToCredentials()
110110

111111
[Test]
112112
[NonParallelizable]
113-
public void ValidateEnvironmentBasedOptionsPassedToCredentials([Values]bool clientIdSpecified, [Values]bool usernameSpecified, [Values]bool tenantIdSpecified)
113+
public void ValidateEnvironmentBasedOptionsPassedToCredentials([Values] bool clientIdSpecified, [Values] bool usernameSpecified, [Values] bool tenantIdSpecified)
114114
{
115115
var expClientId = clientIdSpecified ? Guid.NewGuid().ToString() : null;
116116
var expUsername = usernameSpecified ? Guid.NewGuid().ToString() : null;
@@ -121,9 +121,11 @@ public void ValidateEnvironmentBasedOptionsPassedToCredentials([Values]bool clie
121121
bool onCreateVsCalled = false;
122122
bool onCreateVsCodeCalled = false;
123123

124-
using (new TestEnvVar("AZURE_CLIENT_ID", expClientId))
125-
using (new TestEnvVar("AZURE_USERNAME", expUsername))
126-
using (new TestEnvVar("AZURE_TENANT_ID", expTenantId))
124+
using (new TestEnvVar(new ()
125+
{
126+
{ "AZURE_CLIENT_ID", expClientId },
127+
{ "AZURE_USERNAME", expUsername },
128+
{ "AZURE_TENANT_ID", expTenantId } }))
127129
{
128130
var credFactory = new MockDefaultAzureCredentialFactory(CredentialPipeline.GetInstance(null));
129131

@@ -191,9 +193,11 @@ public void ValidateEmptyEnvironmentBasedOptionsNotPassedToCredentials([Values]
191193
bool onCreateVsCalled = false;
192194
bool onCreateVsCodeCalled = false;
193195

194-
using (new TestEnvVar("AZURE_CLIENT_ID", expClientId))
195-
using (new TestEnvVar("AZURE_USERNAME", expUsername))
196-
using (new TestEnvVar("AZURE_TENANT_ID", expTenantId))
196+
using (new TestEnvVar(new ()
197+
{
198+
{ "AZURE_CLIENT_ID", expClientId },
199+
{ "AZURE_USERNAME", expUsername },
200+
{ "AZURE_TENANT_ID", expTenantId } }))
197201
{
198202
var credFactory = new MockDefaultAzureCredentialFactory(CredentialPipeline.GetInstance(null));
199203

@@ -249,13 +253,13 @@ public void ValidateEmptyEnvironmentBasedOptionsNotPassedToCredentials([Values]
249253
}
250254

251255
[Test]
252-
public void ValidateCtorWithExcludeOptions([Values(true, false)]bool excludeEnvironmentCredential,
253-
[Values(true, false)]bool excludeManagedIdentityCredential,
254-
[Values(true, false)]bool excludeSharedTokenCacheCredential,
255-
[Values(true, false)]bool excludeVisualStudioCredential,
256-
[Values(true, false)]bool excludeVisualStudioCodeCredential,
257-
[Values(true, false)]bool excludeCliCredential,
258-
[Values(true, false)]bool excludeInteractiveBrowserCredential)
256+
public void ValidateCtorWithExcludeOptions([Values(true, false)] bool excludeEnvironmentCredential,
257+
[Values(true, false)] bool excludeManagedIdentityCredential,
258+
[Values(true, false)] bool excludeSharedTokenCacheCredential,
259+
[Values(true, false)] bool excludeVisualStudioCredential,
260+
[Values(true, false)] bool excludeVisualStudioCodeCredential,
261+
[Values(true, false)] bool excludeCliCredential,
262+
[Values(true, false)] bool excludeInteractiveBrowserCredential)
259263
{
260264
var credFactory = new MockDefaultAzureCredentialFactory(CredentialPipeline.GetInstance(null));
261265

@@ -311,13 +315,13 @@ public void ValidateCtorWithExcludeOptions([Values(true, false)]bool excludeEnvi
311315
}
312316

313317
[Test]
314-
public void ValidateAllUnavailable([Values(true, false)]bool excludeEnvironmentCredential,
315-
[Values(true, false)]bool excludeManagedIdentityCredential,
316-
[Values(true, false)]bool excludeSharedTokenCacheCredential,
317-
[Values(true, false)]bool excludeVisualStudioCredential,
318-
[Values(true, false)]bool excludeVisualStudioCodeCredential,
319-
[Values(true, false)]bool excludeCliCredential,
320-
[Values(true, false)]bool excludeInteractiveBrowserCredential)
318+
public void ValidateAllUnavailable([Values(true, false)] bool excludeEnvironmentCredential,
319+
[Values(true, false)] bool excludeManagedIdentityCredential,
320+
[Values(true, false)] bool excludeSharedTokenCacheCredential,
321+
[Values(true, false)] bool excludeVisualStudioCredential,
322+
[Values(true, false)] bool excludeVisualStudioCodeCredential,
323+
[Values(true, false)] bool excludeCliCredential,
324+
[Values(true, false)] bool excludeInteractiveBrowserCredential)
321325
{
322326
if (excludeEnvironmentCredential && excludeManagedIdentityCredential && excludeSharedTokenCacheCredential && excludeVisualStudioCredential && excludeVisualStudioCodeCredential && excludeCliCredential && excludeInteractiveBrowserCredential)
323327
{
@@ -401,7 +405,7 @@ public void ValidateAllUnavailable([Values(true, false)]bool excludeEnvironmentC
401405
}
402406

403407
[Test]
404-
public void ValidateUnhandledException([Values(0, 1, 2, 3, 4, 5, 6)]int exPossition)
408+
public void ValidateUnhandledException([Values(0, 1, 2, 3, 4, 5, 6)] int exPossition)
405409
{
406410
var credFactory = new MockDefaultAzureCredentialFactory(CredentialPipeline.GetInstance(null));
407411

@@ -541,7 +545,7 @@ public void ValidateUnhandledException([Values(0, 1, 2, 3, 4, 5, 6)]int exPossit
541545
}
542546

543547
[Test]
544-
public async Task ValidateSelectedCredentialCaching([Values(typeof(EnvironmentCredential), typeof(ManagedIdentityCredential), typeof(SharedTokenCacheCredential), typeof(VisualStudioCredential), typeof(VisualStudioCodeCredential), typeof(AzureCliCredential), typeof(InteractiveBrowserCredential))]Type availableCredential)
548+
public async Task ValidateSelectedCredentialCaching([Values(typeof(EnvironmentCredential), typeof(ManagedIdentityCredential), typeof(SharedTokenCacheCredential), typeof(VisualStudioCredential), typeof(VisualStudioCodeCredential), typeof(AzureCliCredential), typeof(InteractiveBrowserCredential))] Type availableCredential)
545549
{
546550
var expToken = new AccessToken(Guid.NewGuid().ToString(), DateTimeOffset.MaxValue);
547551

sdk/identity/Azure.Identity/tests/DeviceCodeCredentialTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public async Task AuthenticateWithDeviceCodeMockVerifyCallbackCancellationAsync(
162162

163163
var options = new TokenCredentialOptions() { Transport = mockTransport };
164164

165-
var cancelSource = new CancellationTokenSource(1000);
165+
var cancelSource = new CancellationTokenSource(100);
166166

167167
var cred = InstrumentClient(new DeviceCodeCredential(VerifyDeviceCodeCallbackCancellationToken, ClientId, options: options));
168168

sdk/identity/Azure.Identity/tests/EnvironmentCredentialProviderTests.cs

Lines changed: 23 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Azure.Core.TestFramework;
99
using Azure.Identity.Tests.Mock;
1010
using System.Threading.Tasks;
11+
using System.Collections.Generic;
1112

1213
namespace Azure.Identity.Tests
1314
{
@@ -57,16 +58,13 @@ public void CredentialConstructionClientSecret()
5758
[Test]
5859
public void CredentialConstructionClientCertificate()
5960
{
60-
string clientIdBackup = Environment.GetEnvironmentVariable("AZURE_CLIENT_ID");
61-
string tenantIdBackup = Environment.GetEnvironmentVariable("AZURE_TENANT_ID");
62-
string clientCertificateLocationBackup = Environment.GetEnvironmentVariable("AZURE_CLIENT_CERTIFICATE_PATH");
63-
64-
try
61+
using (new TestEnvVar(new ()
62+
{
63+
{ "AZURE_CLIENT_ID", "mockclientid" },
64+
{ "AZURE_TENANT_ID", "mocktenantid" },
65+
{ "AZURE_CLIENT_CERTIFICATE_PATH", "mockcertificatepath" }
66+
}))
6567
{
66-
Environment.SetEnvironmentVariable("AZURE_CLIENT_ID", "mockclientid");
67-
Environment.SetEnvironmentVariable("AZURE_TENANT_ID", "mocktenantid");
68-
Environment.SetEnvironmentVariable("AZURE_CLIENT_CERTIFICATE_PATH", "mockcertificatepath");
69-
7068
var provider = new EnvironmentCredential();
7169
var cred = provider.Credential as ClientCertificateCredential;
7270
Assert.NotNull(cred);
@@ -78,12 +76,6 @@ public void CredentialConstructionClientCertificate()
7876
Assert.NotNull(certProvider);
7977
Assert.AreEqual("mockcertificatepath", certProvider.CertificatePath);
8078
}
81-
finally
82-
{
83-
Environment.SetEnvironmentVariable("AZURE_CLIENT_ID", clientIdBackup);
84-
Environment.SetEnvironmentVariable("AZURE_TENANT_ID", tenantIdBackup);
85-
Environment.SetEnvironmentVariable("AZURE_CLIENT_CERTIFICATE_PATH", clientCertificateLocationBackup);
86-
}
8779
}
8880

8981
[Test]
@@ -113,52 +105,26 @@ public async Task EnvironmentCredentialAuthenticationFailedException()
113105
await Task.CompletedTask;
114106
}
115107

116-
[NonParallelizable]
117-
[Test]
118-
public void AssertCredentialUnavailableWhenEmptyString()
108+
public static IEnumerable<object[]> AssertCredentialUnavailableWhenEmptyStringEnvironmentSettings()
119109
{
120-
// ensure no env vars are set before starting the test
121-
using (new TestEnvVar("AZURE_CLIENT_ID", null))
122-
using (new TestEnvVar("AZURE_TENANT_ID", null))
123-
using (new TestEnvVar("AZURE_CLIENT_SECRET", null))
124-
using (new TestEnvVar("AZURE_CLIENT_CERTIFICATE_PATH", null))
125-
using (new TestEnvVar("AZURE_USERNAME", null))
126-
using (new TestEnvVar("AZURE_PASSWORD", null))
127-
{
128-
using (new TestEnvVar("AZURE_CLIENT_ID", "mockclientid"))
129-
using (new TestEnvVar("AZURE_CLIENT_SECRET", "mockclientsecret"))
130-
using (new TestEnvVar("AZURE_TENANT_ID", "mocktenantid"))
131-
{
132-
SetEnvVarToEmptyStringAndAssertCredentialUnavailable("AZURE_CLIENT_ID");
133-
SetEnvVarToEmptyStringAndAssertCredentialUnavailable("AZURE_CLIENT_SECRET");
134-
SetEnvVarToEmptyStringAndAssertCredentialUnavailable("AZURE_TENANT_ID");
135-
}
136-
137-
using (new TestEnvVar("AZURE_CLIENT_ID", "mockclientid"))
138-
using (new TestEnvVar("AZURE_CLIENT_CERTIFICATE_PATH", "mockcertpath"))
139-
using (new TestEnvVar("AZURE_TENANT_ID", "mocktenantid"))
140-
{
141-
SetEnvVarToEmptyStringAndAssertCredentialUnavailable("AZURE_CLIENT_ID");
142-
SetEnvVarToEmptyStringAndAssertCredentialUnavailable("AZURE_CLIENT_CERTIFICATE_PATH");
143-
SetEnvVarToEmptyStringAndAssertCredentialUnavailable("AZURE_TENANT_ID");
144-
}
145-
146-
using (new TestEnvVar("AZURE_USERNAME", "mockusername"))
147-
using (new TestEnvVar("AZURE_PASSWORD", "mockpassword"))
148-
using (new TestEnvVar("AZURE_TENANT_ID", "mocktenantid"))
149-
using (new TestEnvVar("AZURE_CLIENT_ID", "mockclientid"))
150-
{
151-
SetEnvVarToEmptyStringAndAssertCredentialUnavailable("AZURE_CLIENT_ID");
152-
SetEnvVarToEmptyStringAndAssertCredentialUnavailable("AZURE_TENANT_ID");
153-
SetEnvVarToEmptyStringAndAssertCredentialUnavailable("AZURE_USERNAME");
154-
SetEnvVarToEmptyStringAndAssertCredentialUnavailable("AZURE_PASSWORD");
155-
}
156-
}
110+
yield return new object[] { new Dictionary<string, string> { { "AZURE_CLIENT_ID", string.Empty }, { "AZURE_CLIENT_SECRET", "mockclientsecret" }, { "AZURE_TENANT_ID", "mocktenantid" } } };
111+
yield return new object[] { new Dictionary<string, string> { { "AZURE_CLIENT_ID", "mockclientid" }, { "AZURE_CLIENT_SECRET", string.Empty }, { "AZURE_TENANT_ID", "mocktenantid" } } };
112+
yield return new object[] { new Dictionary<string, string> { { "AZURE_CLIENT_ID", "mockclientid" }, { "AZURE_CLIENT_SECRET", "mockclientsecret" }, { "AZURE_TENANT_ID", string.Empty } } };
113+
yield return new object[] { new Dictionary<string, string> { { "AZURE_CLIENT_ID", string.Empty }, { "AZURE_CLIENT_CERTIFICATE_PATH", "mockcertpath" }, { "AZURE_TENANT_ID", "mocktenantid" } } };
114+
yield return new object[] { new Dictionary<string, string> { { "AZURE_CLIENT_ID", "mockclientid" }, { "AZURE_CLIENT_CERTIFICATE_PATH", string.Empty }, { "AZURE_TENANT_ID", "mocktenantid" } } };
115+
yield return new object[] { new Dictionary<string, string> { { "AZURE_CLIENT_ID", "mockclientid" }, { "AZURE_CLIENT_CERTIFICATE_PATH", "mockcertpath" }, { "AZURE_TENANT_ID", string.Empty } } };
116+
yield return new object[] { new Dictionary<string, string> { { "AZURE_USERNAME", string.Empty }, { "AZURE_PASSWORD", "mockpassword" }, { "AZURE_TENANT_ID", "mocktenantid" }, { "AZURE_CLIENT_ID", "mockclientid" } } };
117+
yield return new object[] { new Dictionary<string, string> { { "AZURE_USERNAME", "mockusername" }, { "AZURE_PASSWORD", string.Empty }, { "AZURE_TENANT_ID", "mocktenantid" }, { "AZURE_CLIENT_ID", "mockclientid" } } };
118+
yield return new object[] { new Dictionary<string, string> { { "AZURE_USERNAME", "mockusername" }, { "AZURE_PASSWORD", "mockpassword" }, { "AZURE_TENANT_ID", string.Empty }, { "AZURE_CLIENT_ID", "mockclientid" } } };
119+
yield return new object[] { new Dictionary<string, string> { { "AZURE_USERNAME", "mockusername" }, { "AZURE_PASSWORD", "mockpassword" }, { "AZURE_TENANT_ID", "mocktenantid" }, { "AZURE_CLIENT_ID", string.Empty } } };
157120
}
158121

159-
private void SetEnvVarToEmptyStringAndAssertCredentialUnavailable(string envVar)
122+
[NonParallelizable]
123+
[Test]
124+
[TestCaseSource(nameof(AssertCredentialUnavailableWhenEmptyStringEnvironmentSettings))]
125+
public void AssertCredentialUnavailableWhenEmptyString(Dictionary<string, string> environmentVars)
160126
{
161-
using (new TestEnvVar(envVar, string.Empty))
127+
using (new TestEnvVar(environmentVars))
162128
{
163129
var credential = InstrumentClient(new EnvironmentCredential());
164130

sdk/identity/Azure.Identity/tests/ManagedIdentityCredentialLiveTestBase.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.Reflection;
67
using Azure.Core.TestFramework;
78
using Azure.Security.KeyVault.Secrets;
@@ -38,27 +39,24 @@ public void ClearChallengeCacheforRecord()
3839

3940
private class ManagedIdenityEnvironment : IDisposable
4041
{
41-
private readonly TestEnvVar[] _envVars;
42+
private readonly TestEnvVar _envVar;
4243

4344
public ManagedIdenityEnvironment(IdentityTestEnvironment env)
4445
{
45-
_envVars = new TestEnvVar[]
46-
{
47-
new TestEnvVar("IDENTITY_ENDPOINT", env.IdentityEndpoint),
48-
new TestEnvVar("IMDS_ENDPOINT", env.ImdsEndpoint),
49-
new TestEnvVar("MSI_ENDPOINT", env.MsiEndpoint),
50-
new TestEnvVar("MSI_SECRET", env.MsiSecret),
51-
new TestEnvVar("IDENTITY_HEADER", env.IdentityHeader),
52-
new TestEnvVar("IDENTITY_SERVER_THUMBPRINT", env.IdentityServerThumbprint)
53-
};
46+
_envVar =
47+
new TestEnvVar(
48+
new Dictionary<string, string>
49+
{
50+
{ "IDENTITY_ENDPOINT", env.IdentityEndpoint },
51+
{ "IMDS_ENDPOINT", env.ImdsEndpoint },
52+
{ "MSI_ENDPOINT", env.MsiEndpoint },
53+
{ "MSI_SECRET", env.MsiSecret },{ "IDENTITY_HEADER", env.IdentityHeader },
54+
{ "IDENTITY_SERVER_THUMBPRINT", env.IdentityServerThumbprint } });
5455
}
5556

5657
public void Dispose()
5758
{
58-
foreach (TestEnvVar envVar in _envVars)
59-
{
60-
envVar.Dispose();
61-
}
59+
_envVar.Dispose();
6260
}
6361
}
6462
}

0 commit comments

Comments
 (0)