-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Authentication fails only when debugging a pytest. #38094
Comments
From the error message, the Azure CLI executable What terminal does your VS Code use to run tests, and is it the same as the normal terminal you used to run the tests manually? If you print |
Hi @lovettchris. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue. |
Ok, I made sure az is in my PATH for the USER and for SYSTEM and not just for the command line environment, and I get the same error. Here's a small repro: from azure.identity import AzureCliCredential
from azure.storage.blob import BlobServiceClient
def test_azure():
credentials = AzureCliCredential()
storage_account = "srexperiments"
blob_container_name = "unittests"
account_url = f"https://{storage_account}.blob.core.windows.net/"
blob_service_client = BlobServiceClient(account_url, credential=credentials, logging_enable=False)
container_client = blob_service_client.get_container_client(container=blob_container_name)
assert container_client.exists() Steps
|
If you were to have the following code in a separate python script, then use the "Run and Debug" feature in VSCode, does it yield the same error? import subprocess
import os
print(os.environ['PATH'])
args = ["cmd", "/c", "az account --help"]
kwargs = {
"universal_newlines": True,
"env": dict(os.environ, AZURE_CORE_NO_COLOR="true"),
}
try:
output = subprocess.check_output(args, **kwargs)
print(output)
except subprocess.CalledProcessError as e:
print(e) If so, does removing the "cmd" and "/c" entries in the I'm not super familiar with all the VS Code mechanisms like the Debug Console vs Integrated Terminal, but seems like creating/updating a launch.json file could also be something to look into. Perhaps something like this might work: {
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Current File",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"purpose": [ "debug-test" ],
"env": {
"PATH": "${env:PATH};C:\\path\\to\\azure\\cli"
}
}
]
}
|
Hi @lovettchris. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue. |
Your sample code above works fine in the debugger - the cmd runs with the correct path and az command completes. I used "az account show" to be sure it fetches something from azure that requires authentication. --help is not enough. I also tested this in the debugger and it runs fine: import os
import subprocess
def find_az_command():
path = os.environ['PATH']
for p in path.split(os.pathsep):
if os.path.exists(os.path.join(p, 'az.cmd')):
return os.path.join(p, 'az.cmd')
if os.path.exists(os.path.join(p, 'az')):
return os.path.join(p, 'az')
print("ERROR: az command not found in PATH")
return None
def get_account_details():
az = find_az_command()
args = [az, "account", "show"]
kwargs = {
"universal_newlines": True,
"env": dict(os.environ, AZURE_CORE_NO_COLOR="true"),
}
try:
output = subprocess.check_output(args, **kwargs)
return output
except Exception as e:
return str(e)
def test_account_details():
details = get_account_details()
print(details) And debugging this as a pytest in the debugger also succeeds, so not sure why my original test_azure function fails in the debugger during pytest. |
Sorry for the delay in response. I'm still uncertain of what environment conditions cause this. However, I just noticed in the traceback, that this line passes: if shutil.which(EXECUTABLE_NAME) is None:
raise CredentialUnavailableError(message=CLI_NOT_FOUND) This means |
Hi @lovettchris. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue. |
Thanks I'll give that fix a try when it's in, or if there's an easy way to try this fix let me know. |
I believe this should be resolved with the latest release of azure0identity which includes the aforementioned change: #38606 |
Hi @lovettchris. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text "/unresolve" to remove the "issue-addressed" label and continue the conversation. |
azure-common 1.1.28
azure-core 1.31.0
azure-data-tables 12.5.0
azure-identity 1.19.0
azure-keyvault-keys 4.9.0
azure-keyvault-secrets 4.8.0
azure-mgmt-compute 33.0.0
azure-mgmt-core 1.4.0
azure-storage-blob 12.23.1
Describe the bug
I have a simple Azure blob that I'm trying to connect to using DefaultCredentials in a
pytest
and it works fine when I runpytest
from the command line, but when I try and debug thepytest
in VSCode it fails with strange errors.To Reproduce
Steps to reproduce the behavior:
Expected behavior
should work the same
Screenshots
Additional context
Here's the debug log:
The text was updated successfully, but these errors were encountered: