Skip to content

Commit

Permalink
Add NullFastlyCache service, use it in development (pypi#14317)
Browse files Browse the repository at this point in the history
  • Loading branch information
di authored Aug 9, 2023
1 parent 1d5085a commit 5c666c7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dev/environment
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ ARCHIVE_FILES_BACKEND=warehouse.packaging.services.LocalArchiveFileStorage path=
SIMPLE_BACKEND=warehouse.packaging.services.LocalSimpleStorage path=/var/opt/warehouse/simple/ url=http://localhost:9001/simple/{path}
DOCS_BACKEND=warehouse.packaging.services.LocalDocsStorage path=/var/opt/warehouse/docs/
SPONSORLOGOS_BACKEND=warehouse.admin.services.LocalSponsorLogoStorage path=/var/opt/warehouse/sponsorlogos/

ORIGIN_CACHE=warehouse.cache.origin.fastly.NullFastlyCache api_key=some_api_key service_id=some_service_id
MAIL_BACKEND=warehouse.email.services.ConsoleAndSMTPEmailSender host=maildev port=1025 ssl=false [email protected]

BREACHED_EMAILS=warehouse.accounts.NullEmailBreachedService
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/cache/origin/test_fastly.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,3 +610,28 @@ def test_purge_key_no_fallback(

assert cacher._purge_key.calls == _purge_key_calls
assert metrics.increment.calls == []


class TestNullFastlyCache:
def test_purge_key_prints(self, capsys, metrics):
purge_key = pretend.stub(delay=pretend.stub())
request = pretend.stub(
registry=pretend.stub(
settings={
"origin_cache.api_endpoint": "https://api.example.com",
"origin_cache.api_key": "the api key",
"origin_cache.service_id": "the service id",
}
),
task=lambda f: purge_key,
)
cacher = fastly.NullFastlyCache.create_service(None, request)
cacher.purge_key("one", metrics=metrics)

captured = capsys.readouterr()
expected = """
Origin cache purge issued:
* URL: 'https://api.example.com/service/the service id/purge/one'
* Headers: {'Accept': 'application/json', 'Fastly-Key': 'the api key', 'Fastly-Soft-Purge': '1'}
""" # noqa
assert captured.out.strip() == expected.strip()
20 changes: 20 additions & 0 deletions warehouse/cache/origin/fastly.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,23 @@ def purge_key(self, key, metrics=None):
tags=[f"ip_address:{self.api_connect_via}"],
)
self._double_purge_key(key) # Do not connect via on fallback


@implementer(IOriginCache)
class NullFastlyCache(FastlyCache):
"""Same as FastlyCache, but it doesn't issue any requests"""

def _purge_key(self, key, connect_via=None):
path = "/service/{service_id}/purge/{key}".format(
service_id=self.service_id, key=key
)
url = urllib.parse.urljoin(self.api_endpoint, path)
headers = {
"Accept": "application/json",
"Fastly-Key": self.api_key,
"Fastly-Soft-Purge": "1",
}

print("Origin cache purge issued:")
print(f"* URL: {url!r}")
print(f"* Headers: {headers!r}")

0 comments on commit 5c666c7

Please sign in to comment.