Azure client library test utilities
The types and functions in this crate help test client libraries built on azure_core, including the #[recorded::test] attribute.
We use integration tests - tests defined under crates' tests/ directories - for testing against provisioned resources.
Most crates use recorded tests to record (and sanitize) or play back HTTP traffic during test execution by attributing tests with #[recorded::test].
If your crate does not communicate over HTTP or provisioning resources cannot be fully automated, you can also mark tests as #[recorded::test(live)].
This still provides utility such as reading environment variables or other test context that might be helpful when your tests run.
🚨 WARNING 🚨: This project is not supported for anything other than testing Azure client libraries for Rust. The public API and behavior may change at any time.
Prerequisites
- Test Proxy
- (Recommended) PowerShell to easily provision resources
Client methods
To test client methods using our Test Proxy or run against live resources, you can attribute asynchronous tests
using the #[recorded::test] attribute:
use Result;
use ;
use ;
async
The TestContext parameter is required unless your test function is attributed as #[recorded::test(live)] (live-only).
You can name the parameter whatever you want.
The TestContext parameter is used to initialize an HTTP client to play back or record tests
and provides other information to test functions that may be useful.
These tests must also return a std::result::Result<T, E>, which can be redefined e.g., azure_core::Result<T>.
Recording features
Besides instrumenting your client and getting credentials - real credentials when running live or recording,
but mock credentials when playing back - there are a number of other helpful features on the Recording object returned above:
add_sanitizerwill add custom sanitizers. There are many pre-configured by the Test Proxy as well.credentialgets a credential that is appropriate for theTestMode.instrumentconfigures the Test Proxy transport in a providedazure_core::http::ClientOptions.instrument_perfis similar toinstrumentbut configures the transport for performance testing (not benching).randomgets random data (numbers, arrays, etc.) that is initialized from the OS when running live or recording, but the seed is saved with the recording and used during play back so that sequential generation of random data is deterministic. ChaCha20 is used to provide a deterministic, portable sequence of seeded random data.random_stringgenerates a randomStringof a given length with an optional prefix, which is included in the length.remove_sanitizerswill remove named sanitizers, likeAZSDK3430that sanitizes all$..idfields and may cause playback to fail.set_matchersets a custom matcher to compare headers, path segments, and/or body content.skippauses recording until the returned guard is dropped.test_modegets the currentTestMode.vargets a required variable with optionalValueOptionsyou can use to sanitize values. This function will err if the variable is not set in the environment when running live or recording, or available when playing back.var_optgets optional variables and will not err in the aforementioned cases.
Record tests
Like with all our other Azure SDK languages, we use a common system for provisioning resources named Test Resources.
This uses a test-resources.bicep or test-resources.json file in the crate or service directory.
When you run this, it will output some environment variables you can set in your shell, or pass on the command line if your shell supports it.
In this example, it will output a number of environment variables including AZURE_KEYVAULT_URL. We'll pass that in the command line for bash below.
Before you can record, though, you need to make sure you're authenticated. The script above will authenticate you in PowerShell, but the
AzurePowerShellCredential is not yet implemented; however, the AzureCliCredential is so log in with az:
No you can record your tests and, after they pass successfully, check in the test recordings. You need to pass AZURE_TEST_MODE=record to record,
along with any other environment variables output by the Test Resources scripts.
AZURE_TEST_MODE=record AZURE_KEYVAULT_URL=https://my-vault.vault.azure.net
Once your assets are checked in by Test Proxy, you can commit your local changes and create a PR.
Playing back tests
To play back tests, just run cargo test:
If you get errors, they could indicate regressions in your tests or perhaps variables or random data wasn't saved correctly. Review any data you generate or use not coming from the service.
Troubleshooting
Like all Azure SDK client libraries, the azure_core_test crate writes information with the target rooted in the crate name
followed by any modules in which the span was created e.g., azure_core_test::proxy.
Because this crate is used to test Azure SDK client libraries and developers will most likely want to see fewer traces from this crate,
the log levels used are a level lower than typical client libraries:
INFOincludes only important information e.g., the version oftest-proxyand on what port it's listening.DEBUGincludes when a test recording or playback has started and stopped.TRACEincludes details like communications with thetest-proxyitself.
The azure_core_test crate also traces useful information that the test-proxy process itself writes to stdout using the test-proxy target and the TRACE logging level.
You can configure tracing using the RUST_LOG environment variable.
For example, if you wanted to see debug information from all sources by default but see test-proxy messages written to stdout you'd run:
RUST_LOG=debug,test-proxy=trace
ANSI colors are written to the terminal even if redirected to a file. To disable writing ANSI color sequences, pass NO_COLOR=1:
NO_COLOR=1 RUST_LOG=debug,test-proxy=trace