689bac0896
* apply black and prepare for parsing the ci.yml so that we can understand if testproxy is enabled or disabled * The language specific cert trust in Language-Settings has been updated to no longer globally set environment variables SSL_CERT_DIR, SSL_CERT_FILE, and REQUEST_CA_BUNDLE. These are dynamically set in proxy_startup.py |
||
---|---|---|
.. | ||
aio | ||
perfstress_tests | ||
storage | ||
README.md | ||
__init__.py | ||
azure_recorded_testcase.py | ||
azure_testcase.py | ||
cert.py | ||
config.py | ||
envvariable_loader.py | ||
exceptions.py | ||
fake_credentials.py | ||
fake_credentials_async.py | ||
helpers.py | ||
mgmt_recorded_testcase.py | ||
mgmt_settings_fake.py | ||
mgmt_test_stats.py | ||
mgmt_testcase.py | ||
preparers.py | ||
proxy_fixtures.py | ||
proxy_startup.py | ||
proxy_testcase.py | ||
resource_testcase.py | ||
sanitizers.py | ||
setup.py | ||
storage_testcase.py |
README.md
Devtools Testutils
Objects in this package for use with Azure Testing
AzureMgmtPreparer
: Base class for Management-plane resource preparersis_live
: Helper method for determining whether a test run is in live or playback modeget_region_override
: Helper method for determining resource regionEnvironmentVariableLoader
: Preparer for sanitizing secrets from environment variables and delivering them to individual testsRetryCounter
: Object for counting retries on a request.ResponseCallback
: Object for mocking response callbacks.FakeCredential
: Fake credential used for authenticating in playback mode.AsyncFakeCredential
: Fake async credential used for authenticating in playback mode.
Fake test credentials
devtools_testutils
also provides a central location for storing and fetching fake credentials for use in tests:
fake_credentials.py
. Using credentials from this file helps us keep the repository free from
credential leaks and false warnings from the Credential Scanner (CredScan) tool. For more information about
the azure-sdk-for-python
's use of CredScan, please refer to the CredScan monitoring guide.
Use the EnvironmentVariableLoader
Fetching environment variables, passing them directly to tests, and sanitizing their real values can be done all at once
by using the devtools_testutils
EnvironmentVariableLoader
(formerly known as the
PowerShellPreparer
).
This loader is meant to be paired with the PowerShell test resource management commands that are documented in /eng/common/TestResources. It's recommended that all test suites use these scripts for live test resource management.
The EnvironmentVariableLoader
accepts a positional directory
argument and arbitrary keyword-only arguments:
directory
is the name of your package's service as it appears in the Python repository; i.e.service
inazure-sdk-for-python/sdk/service/azure-service-package
.- For example, for
azure-keyvault-keys
, the value ofdirectory
iskeyvault
.
- For example, for
- For each environment variable you want to provide to tests, pass in a keyword argument with the pattern
environment_variable_name="sanitized-value"
.- For example, to fetch the value of
STORAGE_ENDPOINT
and sanitize this value in recordings asfake-endpoint
, providestorage_endpoint="fake-endpoint"
to theEnvironmentVariableLoader
constructor.
- For example, to fetch the value of
Decorated test methods will have the values of environment variables passed to them as keyword arguments, and these values will automatically have sanitizers registered with the test proxy. More specifically, the true values of requested variables will be provided to tests in live mode, and the sanitized values of these variables will be provided in playback mode.
The most common way to use the EnvironmentVariableLoader
is to declare a callable specifying arguments by using
functools.partial
and then decorate test methods with that callable. For example:
import functools
from devtools_testutils import AzureRecordedTestCase, EnvironmentVariableLoader, recorded_by_proxy
ServicePreparer = functools.partial(
EnvironmentVariableLoader,
"service",
service_endpoint="fake-endpoint",
service_account_name="fake-account-name",
)
class TestExample(AzureRecordedTestCase):
@ServicePreparer()
@recorded_by_proxy
def test_example_with_preparer(self, **kwargs):
service_endpoint = kwargs.pop("service_endpoint")
...
Be sure to match the formatting of live values in playback values. For example, if the actual service endpoint in
your .env
file doesn't end with a trailing slash (/
), adding a trailing slash to your playback endpoint value will
result in playback errors. The exact value of your live variables will be replaced with the exact value of your playback
variables in recordings.
Note: The
EnvironmentVariableLoader
expects environment variables for service tests to be prefixed with the service name (e.g.KEYVAULT_
for Key Vault tests). You'll need to set environment variables for{SERVICE}_TENANT_ID
,{SERVICE}_CLIENT_ID
, and{SERVICE}_CLIENT_SECRET
for a service principal when using this class.