Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add robustness for missing TupleComponents #1194

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ def parse_tuple(line: str, line_no: int) -> dict:
return initial_tuple

for component in SINGLE_COMPONENTS:
tuple_component = TupleComponent(component, initial_tuple[component], line_no, is_bulk_path)
if component in initial_tuple:
tuple_component = TupleComponent(component, initial_tuple[component], line_no, is_bulk_path)
else:
logger.info(f"`{component}` was not present on line {line_no}. Skipping component.")
continue

processed_tuple = tuple_component.b64decode().decode_utf8().parse_as_json()
final_value = processed_tuple.final_value
Expand All @@ -201,6 +205,9 @@ def parse_tuple(line: str, line_no: int) -> dict:
logger.error(processed_tuple.error)

for component in LIST_COMPONENTS:
if component not in initial_tuple:
logger.info(f"`{component}` was not present on line {line_no}. Skipping component.")
continue
for i, item in enumerate(initial_tuple[component]):
tuple_component = TupleComponent(f"{component} item {i}", item, line_no, is_bulk_path)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"sourceRequest": {
"Request-URI": "/_snapshot/migration_assistant_repo/rfs-snapshot",
"Method": "DELETE",
"HTTP-Version": "HTTP/1.1",
"payload": {
"inlinedTextBody": ""
}
},
"targetRequest": {
"Request-URI": "/_snapshot/migration_assistant_repo/rfs-snapshot",
"Method": "DELETE",
"payload": {
"inlinedTextBody": ""
}
},
"targetResponses": [
{
"Date": ["Tue, 10 Dec 2024 21:07:03 GMT"],
"Content-Type": ["application/json; charset=UTF-8"],
"Status-Code": 404,
"Reason-Phrase": "Not Found",
"response_time_ms": 49,
"payload": {
"inlinedJsonBody": {}
}
}
],
"connectionId": "024ad2fffe460aed-00000007-00000ac1-61aca27669436ce2-fdb9a98a.1563",
"numRequests": 4,
"numErrors": 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
VALID_TUPLE_GZIPPED_CHUNKED = TEST_DATA_DIRECTORY / "valid_tuple_gzipped_and_chunked.json"
VALID_TUPLE_GZIPPED_CHUNKED_PARSED = TEST_DATA_DIRECTORY / "valid_tuple_gzipped_and_chunked_parsed.json"
INVALID_TUPLE = TEST_DATA_DIRECTORY / "invalid_tuple.json"
VALID_TUPLE_MISSING_COMPONENT = TEST_DATA_DIRECTORY / "valid_tuple_missing_component.json"


def test_get_element_with_regex_succeeds():
Expand Down Expand Up @@ -161,3 +162,14 @@ def test_parse_tuple_with_malformed_bodies(caplog):
assert json.loads(tuple_) == parsed # Values weren't changed if they couldn't be interpreted
assert "Body value of sourceResponse on line 0 could not be decoded to utf-8" in caplog.text
assert "Body value of targetResponses item 0 on line 0 should be a json, but could not be parsed" in caplog.text


def test_parse_tuple_with_missing_component():
with open(VALID_TUPLE_MISSING_COMPONENT, 'r') as f:
tuple_ = f.read()

assert 'sourceResponse' not in json.loads(tuple_)
parsed = parse_tuple(tuple_, 0)

assert 'sourceResponse' not in parsed
assert json.loads(tuple_).keys() == parsed.keys()
Loading