Skip to content

Commit

Permalink
Merge pull request #217 from drepelov/fix_ttl_extraction
Browse files Browse the repository at this point in the history
Fix TTL extraction [RHELDST-20510]
  • Loading branch information
rbikar authored Oct 16, 2023
2 parents 88fba27 + 9571b26 commit 02a744f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
8 changes: 6 additions & 2 deletions tests/test_cdn.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,12 @@ def test_publisher_with_cache_purge(pulp, requests_mock):
setup_fastpurge_mock(requests_mock)

url_ttl = ("https://cdn.example.com/content/unit/1/client/repomd.xml", "33s")

headers = {"X-Cache-Key": f"/fake/cache-key/{url_ttl[1]}/something"}
# ARLs are generated from the template using the {ttl} placeholder, which is replaced with
# the real TTL value. The real TTL value is extracted from the cache key header of the real
# request for the given path using '/(\d+[smhd])/' regex.
# The /1h/foo in the mocked header here is to test that if the path contains a component
# that also matches the TTL regex ('/1h/'), it will still find the correct value ('/33s/').
headers = {"X-Cache-Key": f"/fake/cache-key/{url_ttl[1]}/something/1h/foo"}
requests_mock.register_uri("HEAD", url_ttl[0], headers=headers)

# enqueue repos to publish and wait for publish and purge to finish
Expand Down
5 changes: 3 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ envlist = py38,py39,static,docs,integration
deps=-rtest-requirements.txt
commands=
pytest -v {posargs}
whitelist_externals=sh
allowlist_externals=sh

[testenv:integration]
passenv =
Expand All @@ -16,7 +16,7 @@ passenv =
deps=-rtest-requirements.txt
commands=
pytest -v {posargs} tests/integration
whitelist_externals=yum
allowlist_externals=yum

[testenv:static]
deps=
Expand All @@ -28,6 +28,7 @@ commands=
black --check .
sh -c 'pylint -f colorized ubipop tests; test $(( $? & (1|2|4|32) )) = 0'
allowlist_externals=sh

[testenv:cov]
deps=
-rtest-requirements.txt
Expand Down
4 changes: 2 additions & 2 deletions ubipop/_cdn.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class CdnClient:
_EXPONENT = float(os.environ.get("CDN_RETRY_EXPONENT", "3.0"))
_MAX_SLEEP = float(os.environ.get("CDN_RETRY_MAX_SLEEP", "120.0"))

TTL_REGEX = re.compile(r".*/(\d+[smhd])/.*")
TTL_REGEX = re.compile(r"/(\d+[smhd])/")
CACHE_KEY_HEADER = HeaderPair("akamai-x-get-cache-key", "X-Cache-Key")

def __init__(self, url, arl_templates=None, max_retry_sleep=_MAX_SLEEP, **kwargs):
Expand Down Expand Up @@ -242,7 +242,7 @@ def _get_ttl(self, path):
out = self._get_headers_for_path(path, headers)

def _parse_ttl(value):
parsed = re.match(
parsed = re.search(
self.TTL_REGEX, value.get(self.CACHE_KEY_HEADER.response) or ""
)
return parsed.group(1) if parsed else None
Expand Down

0 comments on commit 02a744f

Please sign in to comment.