@@ -7,12 +7,26 @@ The types and functions in this crate help test client libraries built on `azure
77To test client methods using our [ Test Proxy] or run against live resources, you can attribute asynchronous tests
88using the ` #[recorded::test] ` attribute:
99
10- ``` rust
10+ ``` rust no_run
1111use azure_core :: Result ;
1212use azure_core_test :: {recorded, TestContext };
13+ use azure_security_keyvault_secrets :: {SecretClient , SecretClientOptions };
1314
1415#[recorded:: test]
1516async fn get_secret (ctx : TestContext ) -> Result <()> {
17+ let recording = ctx . recording ();
18+
19+ // Instrument your client options to hook up the test-proxy pipeline policy.
20+ let mut options = SecretClientOptions :: default ();
21+ recording . instrument (& mut options . client_options);
22+
23+ // Get variables, credentials, and pass the instrumented client options to the client to begin.
24+ let client = SecretClient :: new (
25+ recording . var (" AZURE_KEYVAULT_URL" , None ). as_str (),
26+ recording . credential (),
27+ Some (options ),
28+ )? ;
29+
1630 todo! ()
1731}
1832```
@@ -24,4 +38,61 @@ and provides other information to test functions that may be useful.
2438
2539These tests must also return a ` std::result::Result<T, E> ` , which can be redefined e.g., ` azure_core::Result<T> ` .
2640
41+ ### Recording features
42+
43+ Besides instrumenting your client and getting credentials - real credentials when running live or recording,
44+ but mock credentials when playing back - there are a number of other helpful features on the ` Recording ` object returned above:
45+
46+ * ` add_sanitizer ` will add custom sanitizers. There are many pre-configured by the [ Test Proxy] as well.
47+ * ` remove_sanitizers ` will remove named sanitizers, like ` AZSDK3430 ` that sanitizes all ` $..id ` fields and may cause playback to fail.
48+ * ` add_matcher ` adds a custom matcher to match headers, path segments, and or body content.
49+ * ` random ` gets random data (numbers, arrays, etc.) that is initialized from the OS when running live or recording,
50+ but the seed is saved with the recording and used during play back so that sequential generation of random data is deterministic.
51+ ChaCha20 is used to provide a deterministic, portable sequence of seeded random data.
52+ * ` var ` gets a required variable with optional ` ValueOptions ` you can use to sanitize values.
53+ This function will err if the variable is not set in the environment when running live or recording, or available when playing back.
54+ * ` var_opt ` gets optional variables and will not err in the aforementioned cases.
55+
56+ ## Record tests
57+
58+ Like with all our other Azure SDK languages, we use a common system for provisioning resources named [ Test Resources] .
59+ This uses a ` test-resources.json ` or ` test-resources.bicep ` file in the crate or service directory.
60+
61+ 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.
62+
63+ ``` bash
64+ pwsh ./eng/common/TestResources/New-TestResources.ps1 -ServiceDirectory keyvault
65+ ```
66+
67+ 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.
68+
69+ Before you can record, though, you need to make sure you're authenticated. The script above will authenticate you in PowerShell, but the
70+ ` AzurePowerShellCredential ` is not yet implemented; however, the ` AzureCliCredential ` is so log in with ` az ` :
71+
72+ ``` bash
73+ az login
74+ ```
75+
76+ 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,
77+ along with any other environment variables output by the [ Test Resources] scripts.
78+
79+ ``` bash
80+ AZURE_TEST_MODE=record AZURE_KEYVAULT_URL=https://my-vault.vault.azure.net cargo test -p azure_security_keyvault_secrets
81+ test-proxy push -a sdk/keyvault/assets.json
82+ ```
83+
84+ Once your assets are checked in by [ Test Proxy] , you can commit your local changes and create a PR.
85+
86+ ## Playing back tests
87+
88+ To play back tests, just run ` cargo test ` :
89+
90+ ``` bash
91+ cargo test
92+ ```
93+
94+ If you get errors, they could indicate regressions in your tests or perhaps variables or random data wasn't saved correctly.
95+ Review any data you generate or use not coming from the service.
96+
2797[ Test Proxy ] : https://github.com/Azure/azure-sdk-tools/blob/main/tools/test-proxy/Azure.Sdk.Tools.TestProxy/README.md
98+ [ Test Resources ] : https://github.com/Azure/azure-sdk-tools/blob/main/eng/common/TestResources/README.md
0 commit comments