Skip to content

Commit ec78666

Browse files
authored
Support all developer tools for user-based auth in tests (#34927)
1 parent 391fee8 commit ec78666

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

doc/dev/tests.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,12 @@ Both pure ARM templates (`test-resources.json`) and BICEP files (`test-resources
183183

184184
User-based authentication is preferred when using test resources. To enable this:
185185
- Use the [`-UserAuth` command flag][user_auth_flag] when running the `New-TestResources` script.
186-
- Set the environment variable `AZURE_TEST_USE_PWSH_AUTH` to "true" to authenticate with Azure PowerShell, or
187-
`AZURE_TEST_USE_CLI_AUTH` to "true" to authenticate with Azure CLI.
188-
- Ensure you're logged into the tool you choose -- if
186+
- Choose a development tool to authenticate with by setting an `AZURE_TEST_USE_*_AUTH` environment variable to "true" (tests will authenticate as the tool's logged-in user). The following tools are supported, listed in the order that authentication will be attempted in if requested:
187+
1. Azure PowerShell: set `AZURE_TEST_USE_PWSH_AUTH`.
188+
2. Azure CLI (`az`): set `AZURE_TEST_USE_CLI_AUTH`.
189+
3. Visual Studio Code: set `AZURE_TEST_USE_VSCODE_AUTH`.
190+
4. Azure Developer CLI (`azd`): set `AZURE_TEST_USE_AZD_AUTH`.
191+
- Ensure you're logged into the tool you choose -- if
189192
you used `New-TestResources.ps1` to deploy resources, you'll already have logged in with Azure PowerShell.
190193

191194
If you haven't yet set up a `test-resources` file for test resource deployment and/or want to use test resources of
@@ -216,13 +219,15 @@ environment variables necessary to run live tests for the service. After storing
216219
-- formatted as `VARIABLE=value` on separate lines -- your credentials and test configuration variables will be set in
217220
our environment when running tests.
218221

219-
If you used the [`-UserAuth` command flag][user_auth_flag] to deploy test resources, set either
220-
`AZURE_TEST_USE_PWSH_AUTH` or `AZURE_TEST_USE_CLI_AUTH` to "true" to authenticate with Azure PowerShell or Azure CLI,
221-
respectively. If both are set to true, Azure PowerShell will be used.
222+
If you used the [`-UserAuth` command flag][user_auth_flag] to deploy test resources, choose a development tool to
223+
authenticate with by setting an `AZURE_TEST_USE_*_AUTH` environment variable to "true". The following tools are supported, listed in the order that authentication will be attempted in if requested:
224+
1. Azure PowerShell: set `AZURE_TEST_USE_PWSH_AUTH`.
225+
2. Azure CLI (`az`): set `AZURE_TEST_USE_CLI_AUTH`.
226+
3. Visual Studio Code: set `AZURE_TEST_USE_VSCODE_AUTH`.
227+
4. Azure Developer CLI (`azd`): set `AZURE_TEST_USE_AZD_AUTH`.
222228

223229
If your service doesn't have a `test-resources` file for test deployment, you'll need to set environment variables
224-
for authentication at minimum. For user-based authentication, use `AZURE_TEST_USE_PWSH_AUTH` or
225-
`AZURE_TEST_USE_CLI_AUTH` as described above.
230+
for authentication at minimum. For user-based authentication, use `AZURE_TEST_USE_*_AUTH` as described above.
226231

227232
For service principal authentication:
228233
1. Set the `AZURE_SUBSCRIPTION_ID` variable to your organization's subscription ID. You can find it in the "Overview"

tools/azure-sdk-tools/devtools_testutils/azure_recorded_testcase.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ def get_credential(self, client_class, **kwargs):
9393

9494
use_pwsh = os.environ.get("AZURE_TEST_USE_PWSH_AUTH", "false")
9595
use_cli = os.environ.get("AZURE_TEST_USE_CLI_AUTH", "false")
96+
use_vscode = os.environ.get("AZURE_TEST_USE_VSCODE_AUTH", "false")
97+
use_azd = os.environ.get("AZURE_TEST_USE_AZD_AUTH", "false")
9698
is_async = kwargs.pop("is_async", False)
9799

98100
# Return live credentials only in live mode
@@ -107,14 +109,34 @@ def get_credential(self, client_class, **kwargs):
107109
if is_async:
108110
from azure.identity.aio import AzurePowerShellCredential
109111
return AzurePowerShellCredential()
110-
# User-based authentication through Azure CLI, if requested
112+
# User-based authentication through Azure CLI (az), if requested
111113
if use_cli.lower() == "true":
112114
_LOGGER.info("Environment variable AZURE_TEST_USE_CLI_AUTH set to 'true'. Using AzureCliCredential.")
113115
from azure.identity import AzureCliCredential
114116

115117
if is_async:
116118
from azure.identity.aio import AzureCliCredential
117119
return AzureCliCredential()
120+
# User-based authentication through Visual Studio Code, if requested
121+
if use_vscode.lower() == "true":
122+
_LOGGER.info(
123+
"Environment variable AZURE_TEST_USE_VSCODE_AUTH set to 'true'. Using VisualStudioCodeCredential."
124+
)
125+
from azure.identity import VisualStudioCodeCredential
126+
127+
if is_async:
128+
from azure.identity.aio import VisualStudioCodeCredential
129+
return VisualStudioCodeCredential()
130+
# User-based authentication through Azure Developer CLI (azd), if requested
131+
if use_azd.lower() == "true":
132+
_LOGGER.info(
133+
"Environment variable AZURE_TEST_USE_AZD_AUTH set to 'true'. Using AzureDeveloperCliCredential."
134+
)
135+
from azure.identity import AzureDeveloperCliCredential
136+
137+
if is_async:
138+
from azure.identity.aio import AzureDeveloperCliCredential
139+
return AzureDeveloperCliCredential()
118140

119141
# Service principal authentication
120142
if tenant_id and client_id and secret:

0 commit comments

Comments
 (0)