Skip to content

Commit 9ac1d8a

Browse files
authored
[Test proxy] Correctly raise in the event of recording failure (#43738)
1 parent 5906595 commit 9ac1d8a

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

doc/dev/test_proxy_troubleshooting.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ To run recorded tests successfully when recorded values are inconsistent or rand
275275
proxy provides a `variables` API. This makes it possible for a test to record the values of variables that were used
276276
during recording and use the same values in playback mode without a sanitizer.
277277

278+
Note that the recorded variables **must** have string values. For example, trying to record an integer value for a
279+
variable will cause a test proxy error.
280+
278281
For example, imagine that a test uses a randomized `table_uuid` variable when creating resources. The same random value
279282
for `table_uuid` can be used in playback mode by using this `variables` API.
280283

@@ -293,14 +296,14 @@ class TestExample(AzureRecordedTestCase):
293296
@recorded_by_proxy
294297
def test_example(self, **kwargs):
295298
# In live mode, variables is an empty dictionary
296-
# In playback mode, the value of variables is {"table_uuid": "random-value"}
299+
# In playback mode, the value of variables is {"current_time": "<previously recorded time>"}
297300
variables = kwargs.pop("variables", {})
298301

299-
# To fetch variable values, use the `setdefault` method to look for a key ("table_uuid")
300-
# and set a real value for that key if it's not present ("random-value")
301-
table_uuid = variables.setdefault("table_uuid", "random-value")
302+
# To fetch variable values, use the `setdefault` method to look for a key ("current_time")
303+
# and set a real value for that key if it's not present (str(time.time()))
304+
# Note that time.time() is converted from a float to a string to record it properly
305+
current_time = variables.setdefault("current_time", str(time.time()))
302306

303-
# use variables["table_uuid"] when using the table UUID throughout the test
304307
...
305308

306309
# return the variables at the end of the test to record them

eng/tools/azure-sdk-tools/devtools_testutils/proxy_testcase.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -114,32 +114,31 @@ def start_record_or_playback(test_id: str) -> "Tuple[str, Dict[str, str]]":
114114

115115

116116
def stop_record_or_playback(test_id: str, recording_id: str, test_variables: "Dict[str, str]") -> None:
117-
try:
118-
http_client = get_http_client()
119-
if is_live():
120-
http_client.request(
121-
method="POST",
122-
url=RECORDING_STOP_URL,
123-
headers={
124-
"x-recording-file": test_id,
125-
"x-recording-id": recording_id,
126-
"x-recording-save": "true",
127-
"Content-Type": "application/json",
128-
},
129-
# tests don't record successfully unless test_variables is a dictionary
130-
body=json.dumps(test_variables).encode("utf-8") if test_variables else "{}",
131-
)
132-
else:
133-
http_client.request(
134-
method="POST",
135-
url=PLAYBACK_STOP_URL,
136-
headers={"x-recording-id": recording_id},
137-
)
138-
except HTTPError as e:
117+
http_client = get_http_client()
118+
if is_live():
119+
response = http_client.request(
120+
method="POST",
121+
url=RECORDING_STOP_URL,
122+
headers={
123+
"x-recording-file": test_id,
124+
"x-recording-id": recording_id,
125+
"x-recording-save": "true",
126+
"Content-Type": "application/json",
127+
},
128+
# tests don't record successfully unless test_variables is a dictionary
129+
body=json.dumps(test_variables or {}).encode("utf-8"),
130+
)
131+
else:
132+
response = http_client.request(
133+
method="POST",
134+
url=PLAYBACK_STOP_URL,
135+
headers={"x-recording-id": recording_id},
136+
)
137+
if response.status >= 400:
139138
raise HttpResponseError(
140139
"The test proxy ran into an error while ending the session. Make sure any test variables you record have "
141-
"string values."
142-
) from e
140+
f"string values. Full error details: {response.data}"
141+
)
143142

144143

145144
def get_proxy_netloc() -> "Dict[str, str]":

0 commit comments

Comments
 (0)