Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Dec 12, 2024
1 parent c1e2303 commit 5b65de9
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 88 deletions.
55 changes: 37 additions & 18 deletions exodus_lambda/functions/origin_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,15 @@ def uri_alias(self, uri, aliases, ignore_exclusions=False):
processed = []

for alias in remaining:
exclusion_match = not ignore_exclusions and \
any([re.search(exclusion, uri) for exclusion
in alias.get("exclude_paths",[])])
if (uri.startswith(alias["src"] + "/") or uri == alias["src"]) \
and not exclusion_match:
exclusion_match = not ignore_exclusions and any(
[
re.search(exclusion, uri)
for exclusion in alias.get("exclude_paths", [])
]
)
if (
uri.startswith(alias["src"] + "/") or uri == alias["src"]
) and not exclusion_match:
uri = uri.replace(alias["src"], alias["dest"], 1)
processed.append(alias)

Expand All @@ -125,17 +129,20 @@ def uri_alias(self, uri, aliases, ignore_exclusions=False):
def resolve_aliases(self, uri, ignore_exclusions=False):
# aliases relating to origin, e.g. content/origin <=> origin

uri = self.uri_alias(uri, self.definitions.get("origin_alias"),
ignore_exclusions)
uri = self.uri_alias(
uri, self.definitions.get("origin_alias"), ignore_exclusions
)
# aliases relating to rhui; listing files are a special exemption
# because they must be allowed to differ for rhui vs non-rhui.
if not uri.endswith("/listing"):
uri = self.uri_alias(uri, self.definitions.get("rhui_alias"),
ignore_exclusions)
uri = self.uri_alias(
uri, self.definitions.get("rhui_alias"), ignore_exclusions
)

# aliases relating to releasever; e.g. /content/dist/rhel8/8 <=> /content/dist/rhel8/8.5
uri = self.uri_alias(uri, self.definitions.get("releasever_alias"),
ignore_exclusions)
uri = self.uri_alias(
uri, self.definitions.get("releasever_alias"), ignore_exclusions
)

self.logger.debug("Resolved request URI: %s", uri)

Expand Down Expand Up @@ -377,15 +384,20 @@ def handler(self, event, context):
return self.handle_cookie_request(event)

uri = self.resolve_aliases(request["uri"])
fallback_uri = self.resolve_aliases(request["uri"],
ignore_exclusions=True)
fallback_uri = self.resolve_aliases(
request["uri"], ignore_exclusions=True
)
fallback_uri = fallback_uri if uri != fallback_uri else None
if listing_response := self.handle_listing_request(uri):
self.set_cache_control(uri, listing_response)
return listing_response
if fallback_uri and fallback_uri.endswith("/listing"):
self.logger.info("Listing could not be found for '%s'."
" Attempting fallback '%s'", uri, fallback_uri)
self.logger.info(
"Listing could not be found for '%s'."
" Attempting fallback '%s'",
uri,
fallback_uri,
)
if listing_response := self.handle_listing_request(fallback_uri):
self.set_cache_control(fallback_uri, listing_response)
return listing_response
Expand All @@ -394,9 +406,16 @@ def handler(self, event, context):
if out := self.handle_file_request(request, table, original_uri, uri):
return out
if fallback_uri:
self.logger.info("No items for '%s' in table '%s' were found. "
"Trying fallback URI '%s'.", uri, table, fallback_uri)
if out := self.handle_file_request(request, table, original_uri, fallback_uri):
self.logger.info(
"No items for '%s' in table '%s' were found. "
"Trying fallback URI '%s'.",
uri,
table,
fallback_uri,
)
if out := self.handle_file_request(
request, table, original_uri, fallback_uri
):
return out

self.logger.info("No item found for URI: %s", uri)
Expand Down
133 changes: 83 additions & 50 deletions tests/functions/test_alias.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# More in depth tests for alias resolution.
from collections import namedtuple

import pytest

from exodus_lambda.functions.origin_request import OriginRequest
Expand Down Expand Up @@ -53,69 +54,101 @@ def test_alias_equal():
"uri, expected_uri, ignore_exclusions, aliases",
[
(
"/origin/path/dir/filename.ext",
"/origin/path/dir/filename.ext",
False,
[{"src": "/origin/path", "dest": "/alias",
"exclude_paths": ["/dir/"]}],
"/origin/path/dir/filename.ext",
"/origin/path/dir/filename.ext",
False,
[
{
"src": "/origin/path",
"dest": "/alias",
"exclude_paths": ["/dir/"],
}
],
),
(
"/origin/path/dir/filename.ext",
"/alias/dir/filename.ext",
False,
[{"src": "/origin/path", "dest": "/alias",
"exclude_paths": ["/banana/"]}],
"/origin/path/dir/filename.ext",
"/alias/dir/filename.ext",
False,
[
{
"src": "/origin/path",
"dest": "/alias",
"exclude_paths": ["/banana/"],
}
],
),
(
"/origin/path/c/dir/filename.ext",
"/second/step/c/dir/filename.ext",
False,
[
{"src": "/origin/path", "dest": "/first/step", "exclude_paths": ["/a/"]},
{"src": "/first", "dest": "/second", "exclude_paths": ["/b/"]},
{"src": "/second", "dest": "/third", "exclude_paths": ["/c/"]}
],
"/origin/path/c/dir/filename.ext",
"/second/step/c/dir/filename.ext",
False,
[
{
"src": "/origin/path",
"dest": "/first/step",
"exclude_paths": ["/a/"],
},
{"src": "/first", "dest": "/second", "exclude_paths": ["/b/"]},
{"src": "/second", "dest": "/third", "exclude_paths": ["/c/"]},
],
),
(
"/origin/path/rhel7/dir/filename.ext",
"/aliased/path/rhel7/dir/filename.ext",
False,
[
{"src": "/origin", "dest": "/aliased",
"exclude_paths": ["/rhel[89]/"]},
],
"/origin/path/rhel7/dir/filename.ext",
"/aliased/path/rhel7/dir/filename.ext",
False,
[
{
"src": "/origin",
"dest": "/aliased",
"exclude_paths": ["/rhel[89]/"],
},
],
),
(
"/origin/path/rhel9/dir/filename.ext",
"/origin/path/rhel9/dir/filename.ext",
False,
[
{"src": "/origin", "dest": "/aliased",
"exclude_paths": ["/rhel[89]/"]},
],
"/origin/path/rhel9/dir/filename.ext",
"/origin/path/rhel9/dir/filename.ext",
False,
[
{
"src": "/origin",
"dest": "/aliased",
"exclude_paths": ["/rhel[89]/"],
},
],
),
(
"/origin/path/rhel9/dir/filename.ext",
"/aliased/path/rhel9/dir/filename.ext",
True,
[
{"src": "/origin", "dest": "/aliased",
"exclude_paths": ["/rhel9/"]},
],
"/origin/path/rhel9/dir/filename.ext",
"/aliased/path/rhel9/dir/filename.ext",
True,
[
{
"src": "/origin",
"dest": "/aliased",
"exclude_paths": ["/rhel9/"],
},
],
),
(
"/origin/path/rhel9/dir/filename.ext",
"/aliased/path/rhel9/dir/filename.ext",
True,
[
{"src": "/origin", "dest": "/aliased",
"exclude_paths": ["/rhel7/"]},
],
(
"/origin/path/rhel9/dir/filename.ext",
"/aliased/path/rhel9/dir/filename.ext",
True,
[
{
"src": "/origin",
"dest": "/aliased",
"exclude_paths": ["/rhel7/"],
},
],
),
],
ids=["excluded", "not excluded", "multilevel", "regex not excluded",
"regex excluded", "exclusion ignored matching",
"exclusion ignored no match"],
ids=[
"excluded",
"not excluded",
"multilevel",
"regex not excluded",
"regex excluded",
"exclusion ignored matching",
"exclusion ignored no match",
],
)
def test_alias_exclusions(uri, expected_uri, ignore_exclusions, aliases):
"""Paths exactly matching an alias can be resolved."""
Expand Down
54 changes: 34 additions & 20 deletions tests/functions/test_origin_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,22 +498,26 @@ def test_origin_request_listing_fallback(mocked_cache, caplog):
req_uri = "/content/dist/rhel/myOwnRHEL/listing"
real_uri = "/content/dist/rhel/extraSpecialRHEL/listing"
definitions = mock_definitions()
definitions["listing"]["/content/dist/rhel/extraSpecialRHEL"]\
= {"var": "basearch", "values": ["x86_64"]}
definitions["listing"]["/content/dist/rhel/extraSpecialRHEL"] = {
"var": "basearch",
"values": ["x86_64"],
}
test_alias = {
"src": "/content/dist/rhel/myOwnRHEL",
"dest": "/content/dist/rhel/extraSpecialRHEL",
"exclude_paths": ["listing"]
}
"src": "/content/dist/rhel/myOwnRHEL",
"dest": "/content/dist/rhel/extraSpecialRHEL",
"exclude_paths": ["listing"],
}
definitions["releasever_alias"].append(test_alias)
mocked_cache.TTLCache.return_value = {"exodus-config": definitions}

event = {"Records": [{"cf": {"request": {"uri": req_uri, "headers": {}}}}]}
request = OriginRequest(conf_file=TEST_CONF).handler(event, context=None)

assert f"Handling listing request: {req_uri}" in caplog.text
assert f"Listing could not be found for '{req_uri}'. " \
f"Attempting fallback '{real_uri}'" in caplog.text
assert (
f"Listing could not be found for '{req_uri}'. "
f"Attempting fallback '{real_uri}'" in caplog.text
)
assert f"Handling listing request: {real_uri}" in caplog.text
# It should successfully generate appropriate listing response.
assert request == {
Expand All @@ -528,6 +532,7 @@ def test_origin_request_listing_fallback(mocked_cache, caplog):
},
}


@mock.patch("boto3.client")
@mock.patch("exodus_lambda.functions.origin_request.cachetools")
@mock.patch("exodus_lambda.functions.origin_request.datetime")
Expand Down Expand Up @@ -933,18 +938,25 @@ def test_fallback_uri(
"object_key": {"S": "e4a3f2sum"},
}
]
}
},
]
expected_boto_calls = [
mock.call(TableName='test-table', Limit=1, ConsistentRead=True,
ScanIndexForward=False,
KeyConditionExpression='web_uri = :u and from_date <= :d',
ExpressionAttributeValues={
':u': {'S': uri},
':d': {'S': '2020-02-17T15:38:05.864+00:00'}}) for uri in
["/content/dist/rhel8/8/files/deletion.iso",
"/content/dist/rhel8/8/files/deletion.iso/.__exodus_autoindex",
"/content/dist/rhel8/8.5/files/deletion.iso"]
mock.call(
TableName="test-table",
Limit=1,
ConsistentRead=True,
ScanIndexForward=False,
KeyConditionExpression="web_uri = :u and from_date <= :d",
ExpressionAttributeValues={
":u": {"S": uri},
":d": {"S": "2020-02-17T15:38:05.864+00:00"},
},
)
for uri in [
"/content/dist/rhel8/8/files/deletion.iso",
"/content/dist/rhel8/8/files/deletion.iso/.__exodus_autoindex",
"/content/dist/rhel8/8.5/files/deletion.iso",
]
]
event = {"Records": [{"cf": {"request": {"uri": req_uri, "headers": {}}}}]}

Expand All @@ -954,8 +966,10 @@ def test_fallback_uri(
).handler(event, context=None)

assert f"Item found for URI: {real_uri}" in caplog.text
assert f"No items for '{req_uri}' in table 'test-table' were found. " \
f"Trying fallback URI '{real_uri}'." in caplog.text
assert (
f"No items for '{req_uri}' in table 'test-table' were found. "
f"Trying fallback URI '{real_uri}'." in caplog.text
)

mocked_boto3_client().query.assert_has_calls(expected_boto_calls)
assert request == {
Expand Down

0 comments on commit 5b65de9

Please sign in to comment.