You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/dev/test_proxy_migration_guide.md
+98-31
Original file line number
Diff line number
Diff line change
@@ -95,8 +95,8 @@ Resource preparers need a management client to function, so test classes that us
95
95
96
96
### Perform one-time setup
97
97
98
-
1. Docker is a requirement for using the test proxy. You can install Docker from [docs.docker.com][docker_install].
99
-
2. After installing, make sure Docker is running and is using Linux containers before running tests.
98
+
1. Docker (or Podman) is a requirement for using the test proxy. You can install Docker from [docs.docker.com][docker_install], or install Podman at [podman.io][podman]. To use Podman, set an alias for `podman` to replace the `docker` command.
99
+
2. After installing, make sure Docker/Podman is running and is using Linux containers before running tests.
100
100
3. Follow the instructions [here][proxy_cert_docs] to complete setup. You need to trust a certificate on your machine in
101
101
order to communicate with the test proxy over a secure connection.
102
102
@@ -110,6 +110,7 @@ In a `conftest.py` file for your package's tests, add a session-level fixture th
110
110
`devtools_testutils.test_proxy` as a parameter (and has `autouse` set to `True`):
111
111
112
112
```python
113
+
import pytest
113
114
from devtools_testutils import test_proxy
114
115
115
116
# autouse=True will trigger this fixture on each pytest run, even if it's not explicitly used by a test method
@@ -119,9 +120,7 @@ def start_proxy(test_proxy):
119
120
```
120
121
121
122
The `test_proxy` fixture will fetch the test proxy Docker image and create a new container called
122
-
`ambitious_azsdk_test_proxy` if one doesn't exist already. If the container already exists, the fixture will start the
123
-
container if it's currently stopped. The container will be stopped after tests finish running, but will stay running if
124
-
test execution is interrupted.
123
+
`ambitious_azsdk_test_proxy`, which will be deleted after test execution unless interrupted.
125
124
126
125
If your tests already use an `autouse`d, session-level fixture for tests, you can accept the `test_proxy` parameter in
127
126
that existing fixture instead of adding a new one. For an example, see the [Register sanitizers](#register-sanitizers)
@@ -144,26 +143,22 @@ need old `.yml` recordings.
144
143
> **Note:** support for configuring live or playback tests with a `testsettings_local.cfg` file has been
145
144
> deprecated in favor of using just `AZURE_TEST_RUN_LIVE`.
146
145
147
-
> **Note:** the recording storage location is determined when the proxy Docker container is created. If there are
148
-
> multiple local copies of the `azure-sdk-for-python` repo on your machine, you will need to delete any existing
149
-
> `ambitious_azsdk_test_proxy` container before recordings can be stored in a different repo copy.
150
-
151
146
### Register sanitizers
152
147
153
148
Since the test proxy doesn't use [`vcrpy`][vcrpy], tests don't use a scrubber to sanitize values in recordings.
154
149
Instead, sanitizers (as well as matchers and transforms) can be registered on the proxy as detailed in
155
150
[this][sanitizers] section of the proxy documentation. Sanitizers can be registered via `add_*_sanitizer` methods in
156
151
`devtools_testutils`. For example, the general-use method for sanitizing recording bodies, headers, and URIs is
157
-
`add_general_regex_sanitizer`. Other sanitizers are available for more specific scenarios and can be found at
152
+
`add_general_string_sanitizer`. Other sanitizers are available for more specific scenarios and can be found at
Note that the sanitizer fixture accepts the `test_proxy` fixture as a parameter to ensure the proxy is started
192
189
beforehand.
193
190
194
191
For a more advanced scenario, where we want to sanitize the account names of all Tables endpoints in recordings, we
195
-
could instead call
192
+
could instead use the `add_general_regex_sanitizer` method:
196
193
197
194
```python
198
195
add_general_regex_sanitizer(
@@ -267,32 +264,40 @@ This loader is meant to be paired with the PowerShell test resource management c
267
264
[/eng/common/TestResources][test_resources]. It's recommended that all test suites use these scripts for live test
268
265
resource management.
269
266
270
-
For an example of using the EnvironmentVariableLoader with the test proxy, you can refer to the Tables SDK. The
271
-
CosmosPreparer and TablesPreparer defined in this [preparers.py][tables_preparers] file each define an instance of the
272
-
EnvironmentVariableLoader, which are used to fetch environment variables for Cosmos and Tables, respectively. These
273
-
preparers can be used to decorate test methods directly; for example:
267
+
The EnvironmentVariableLoader accepts a positional `directory` argument and arbitrary keyword-only arguments:
268
+
-`directory` is the name of your package's service as it appears in the Python repository; i.e. `service` in `azure-sdk-for-python/sdk/service/azure-service-package`.
269
+
- For example, for `azure-keyvault-keys`, the value of `directory` is `keyvault`.
270
+
- For each environment variable you want to provide to tests, pass in a keyword argument with the pattern `environment_variable_name="sanitized-value"`.
271
+
- For example, to fetch the value of `STORAGE_ENDPOINT` and sanitize this value in recordings as `fake-endpoint`, provide `storage_endpoint="fake-endpoint"` to the EnvironmentVariableLoader constructor.
272
+
273
+
Decorated test methods will have the values of environment variables passed to them as keyword arguments, and these
274
+
values will automatically have sanitizers registered with the test proxy. More specifically, the true values of
275
+
requested variables will be provided to tests in live mode, and the sanitized values of these variables will be provided
276
+
in playback mode.
277
+
278
+
The most common way to use the EnvironmentVariableLoader is to declare a callable specifying arguments by using
279
+
`functools.partial` and then decorate test methods with that callable. For example:
274
280
275
281
```python
276
-
from devtools_testutils import AzureRecordedTestCase, recorded_by_proxy
277
-
from .preparers import TablesPreparer
282
+
import functools
283
+
from devtools_testutils import AzureRecordedTestCase, EnvironmentVariableLoader, recorded_by_proxy
Copy file name to clipboardExpand all lines: doc/dev/tests.md
+10-8
Original file line number
Diff line number
Diff line change
@@ -151,8 +151,8 @@ To migrate an existing test suite to use the test proxy, or to learn more about
151
151
152
152
### Perform one-time test proxy setup
153
153
154
-
1. Docker is a requirement for using the test proxy. You can install Docker from [docs.docker.com][docker_install].
155
-
2. After installing, make sure Docker is running and is using Linux containers before running tests.
154
+
1. Docker (or Podman) is a requirement for using the test proxy. You can install Docker from [docs.docker.com][docker_install], or install Podman at [podman.io][podman]. To use Podman, set an alias for `podman` to replace the `docker` command.
155
+
2. After installing, make sure Docker/Podman is running and is using Linux containers before running tests.
156
156
3. Follow the instructions [here][proxy_cert_docs] to complete setup. You need to trust a certificate on your machine in
157
157
order to communicate with the test proxy over a secure connection.
158
158
@@ -213,6 +213,7 @@ Create a `conftest.py` file within your package's test directory (`sdk/{service}
213
213
session-level fixture that accepts `devtools_testutils.test_proxy` as a parameter (and has `autouse` set to `True`):
214
214
215
215
```python
216
+
import pytest
216
217
from devtools_testutils import test_proxy
217
218
218
219
# autouse=True will trigger this fixture on each pytest run, even if it's not explicitly used by a test method
@@ -358,27 +359,27 @@ There are two primary ways to keep secrets from being written into recordings:
358
359
1. The `EnvironmentVariableLoader` will automatically sanitize the values of captured environment variables with the
359
360
provided fake values.
360
361
2. Sanitizers can be registered via `add_*_sanitizer` methods in `devtools_testutils`. For example, the general-use
361
-
method for sanitizing recording bodies, headers, and URIs is `add_general_regex_sanitizer`. Other sanitizers are
362
+
method for sanitizing recording bodies, headers, and URIs is `add_general_string_sanitizer`. Other sanitizers are
362
363
available for more specific scenarios and can be found at [devtools_testutils/sanitizers.py][py_sanitizers].
363
364
364
365
As a simple example of registering a sanitizer, you can provide the exact value you want to sanitize from recordings as
365
-
the `regex` in the general regex sanitizer. To replace all instances of the string "my-key-vault" with "fake-vault" in
366
+
the `target` in the general string sanitizer. To replace all instances of the string "my-key-vault" with "fake-vault" in
366
367
recordings, you could add something like the following in the package's `conftest.py` file:
367
368
368
369
```python
369
-
from devtools_testutils importadd_general_regex_sanitizer, test_proxy
370
+
from devtools_testutils importadd_general_string_sanitizer, test_proxy
370
371
371
372
# autouse=True will trigger this fixture on each pytest run, even if it's not explicitly used by a test method
0 commit comments