Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
oittaa committed Feb 13, 2021
1 parent 6ff19b7 commit 13a87bd
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 19 deletions.
5 changes: 0 additions & 5 deletions .travis.yml
Expand Up @@ -2,22 +2,17 @@ os: linux
dist: bionic
sudo: false
language: python
env:
- STORAGE_EMULATOR_HOST="http://localhost:8080"
python:
- "3.5"
- "3.6"
- "3.7"
- "3.8"
- "3.9"
- "pypy3"
services:
- docker
install:
- pip install -r requirements.txt
- pip install coveralls
- pip install --editable .
- mkdir -p examples/emulated_storage/test-bucket && docker run -d --name fake-gcs-server -p 8080:8080 -v ${PWD}/examples/emulated_storage:/data fsouza/fake-gcs-server -scheme http -port 8080
script: py.test
after_success:
- coveralls
Expand Down
11 changes: 6 additions & 5 deletions flask_caching/backends/googlecloudstoragecache.py
Expand Up @@ -3,8 +3,6 @@
import json
import logging

from google.auth.credentials import AnonymousCredentials
from google.cloud import storage, exceptions
from flask_caching.backends.base import BaseCache


Expand Down Expand Up @@ -47,6 +45,11 @@ def __init__(
super(GoogleCloudStorageCache, self).__init__(default_timeout)
if not isinstance(bucket, str):
raise ValueError("GCSCache bucket parameter must be a string")
try:
from google.auth.credentials import AnonymousCredentials
from google.cloud import storage, exceptions
except ImportError:
raise RuntimeError("no google.cloud module found")
if anonymous:
self._client = storage.Client(
credentials=AnonymousCredentials(), project="test", **kwargs
Expand Down Expand Up @@ -87,15 +90,13 @@ def set(self, key, value, timeout=None):
except (UnicodeDecodeError, TypeError):
content_type = "application/octet-stream"
blob = self.bucket.blob(full_key)
blob.upload_from_string(value, content_type=content_type)
if timeout is None:
timeout = self.default_timeout
if timeout != 0:
# Use 'Custom-Time' for expiry
# https://cloud.google.com/storage/docs/metadata#custom-time
blob.reload()
blob.custom_time = self._now(delta=timeout)
blob.patch()
blob.upload_from_string(value, content_type=content_type)
logger.debug("set key %r", full_key)
return True

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -7,3 +7,4 @@ pytest-xprocess
redis
pylibmc
google-cloud-storage
gcp-storage-emulator
10 changes: 9 additions & 1 deletion setup.py
Expand Up @@ -84,7 +84,15 @@ def run_tests(self):
platforms="any",
python_requires=">=3.5",
install_requires=["Flask"],
tests_require=["pytest", "pytest-cov", "pytest-xprocess", "pylibmc", "redis"],
tests_require=[
"pytest",
"pytest-cov",
"pytest-xprocess",
"pylibmc",
"redis",
"google-cloud-storage",
"gcp-storage-emulator",
],
cmdclass={"test": PyTest},
classifiers=[
"Development Status :: 5 - Production/Stable",
Expand Down
13 changes: 9 additions & 4 deletions tests/conftest.py
Expand Up @@ -121,6 +121,10 @@ class Starter(ProcessStarter):

@pytest.fixture(scope="class")
def gcs_bucket(request):
try:
from google.cloud import storage
except ImportError:
pytest.skip("Python package 'google-cloud-storage' is not installed.")
bucket = os.environ.get("CACHE_GCS_BUCKET")
if not bucket:
pytest.skip("'CACHE_GCS_BUCKET' environment variable is not set.")
Expand All @@ -133,9 +137,10 @@ def gcs_bucket(request):

@pytest.fixture(scope="class")
def gcs_emulator(request):
emulator = os.environ.get("STORAGE_EMULATOR_HOST")
if not emulator:
pytest.skip("'STORAGE_EMULATOR_HOST' not set.")
try:
from gcp_storage_emulator.server import create_server
except ImportError:
pytest.skip("Python package 'gcp-storage-emulator' is not installed.")
if sys.version_info.major == 3 and sys.version_info.minor < 6:
pytest.skip("Obsolete Python version.")
return emulator
return create_server
28 changes: 24 additions & 4 deletions tests/test_backend_cache.py
Expand Up @@ -9,6 +9,7 @@
:license: BSD, see LICENSE for more details.
"""
import errno
import os
import time

import pytest
Expand All @@ -31,6 +32,17 @@
except ImportError:
memcache = None

try:
from google.cloud import storage, exceptions
except ImportError:
storage = None
exceptions = None

try:
from gcp_storage_emulator.server import create_server
except ImportError:
create_server = None


class CacheTestsBase(object):
_can_use_fast_sleep = True
Expand Down Expand Up @@ -357,18 +369,26 @@ def _delete_many(self, keys):
return True


# _guaranteed_deletes disabled, because emulator doesn't handle blob.custom_time correctly.
class TestFakeGoogleCloudStorageCache(GenericCacheTests):
_guaranteed_deletes = False

@pytest.fixture(scope="class", autouse=True)
def requirements(self, gcs_emulator):
pass

@pytest.fixture
def make_cache(self):
def make_cache(self, gcs_emulator):
orig_env = os.environ.get("STORAGE_EMULATOR_HOST")
os.environ["STORAGE_EMULATOR_HOST"] = "http://localhost:9023"
server = gcs_emulator(
"localhost", 9023, in_memory=True, default_bucket="test-bucket"
)
server.start()
c = FakeGoogleCloudStorageCache(
bucket="test-bucket", delete_expired_objects_on_read=True, anonymous=True
)
yield lambda: c
c.clear()
server.stop()
if orig_env is None:
del os.environ["STORAGE_EMULATOR_HOST"]
else:
os.environ["STORAGE_EMULATOR_HOST"] = orig_env

0 comments on commit 13a87bd

Please sign in to comment.