| 1 |
import pytest |
| 2 |
from clear import ClearTests |
| 3 |
from common import CommonTests |
| 4 |
from delete_many_with_prefix import DeleteManyWithPrefixTests |
| 5 |
from has import HasTests |
| 6 |
from serializer import SerializerTests |
| 7 |
|
| 8 |
from cachelib import DynamoDbCache |
| 9 |
|
| 10 |
|
| 11 |
@pytest.fixture(autouse=True, params=[DynamoDbCache]) |
| 12 |
def cache_factory(request, key_prefix): |
| 13 |
import warnings |
| 14 |
|
| 15 |
warnings.filterwarnings( |
| 16 |
action="ignore", message="unclosed", category=ResourceWarning |
| 17 |
) |
| 18 |
|
| 19 |
def _factory(self, *args, **kwargs): |
| 20 |
import os |
| 21 |
|
| 22 |
os.environ.setdefault("AWS_ACCESS_KEY_ID", "RANDOM") |
| 23 |
os.environ.setdefault("AWS_SECRET_ACCESS_KEY", "RANDOM") |
| 24 |
kwargs["endpoint_url"] = "http://localhost:8000" |
| 25 |
kwargs["region_name"] = "us-west-2" |
| 26 |
kwargs.setdefault("key_prefix", key_prefix) |
| 27 |
rc = request.param(*args, **kwargs) |
| 28 |
rc.clear() |
| 29 |
return rc |
| 30 |
|
| 31 |
if request.cls: |
| 32 |
request.cls.cache_factory = _factory |
| 33 |
|
| 34 |
|
| 35 |
@pytest.mark.network |
| 36 |
class TestDynamoDbCache( |
| 37 |
CommonTests, ClearTests, HasTests, DeleteManyWithPrefixTests, SerializerTests |
| 38 |
): |
| 39 |
pass |
| 40 |
|
| 41 |
|
| 42 |
def _fast_fail_config(): |
| 43 |
import os |
| 44 |
|
| 45 |
from botocore.config import Config |
| 46 |
|
| 47 |
os.environ.setdefault("AWS_ACCESS_KEY_ID", "RANDOM") |
| 48 |
os.environ.setdefault("AWS_SECRET_ACCESS_KEY", "RANDOM") |
| 49 |
return Config(connect_timeout=0.5, read_timeout=0.5, retries={"max_attempts": 0}) |
| 50 |
|
| 51 |
|
| 52 |
class TestDynamoDbCacheCheckConnection: |
| 53 |
# nothing listens on port 1, so load() fails with a BotoCoreError |
| 54 |
def test_unreachable_server_raises_when_enabled(self): |
| 55 |
with pytest.raises(RuntimeError, match="could not connect to DynamoDB"): |
| 56 |
DynamoDbCache( |
| 57 |
endpoint_url="http://127.0.0.1:1", |
| 58 |
region_name="us-west-2", |
| 59 |
check_connection=True, |
| 60 |
config=_fast_fail_config(), |
| 61 |
) |
| 62 |
|
| 63 |
def test_unreachable_server_is_lazy_by_default(self): |
| 64 |
cache = DynamoDbCache( |
| 65 |
endpoint_url="http://127.0.0.1:1", |
| 66 |
region_name="us-west-2", |
| 67 |
config=_fast_fail_config(), |
| 68 |
) |
| 69 |
assert isinstance(cache, DynamoDbCache) |
| 70 |
|
| 71 |
|
| 72 |
@pytest.mark.network |
| 73 |
class TestDynamoDbCacheCheckConnectionLive: |
| 74 |
@pytest.mark.parametrize("check_connection", [True, False]) |
| 75 |
def test_missing_table_is_created_regardless_of_flag(self, check_connection): |
| 76 |
import uuid |
| 77 |
|
| 78 |
table_name = f"check-connection-{uuid.uuid4().hex}" |
| 79 |
cache = DynamoDbCache( |
| 80 |
table_name=table_name, |
| 81 |
endpoint_url="http://localhost:8000", |
| 82 |
region_name="us-west-2", |
| 83 |
check_connection=check_connection, |
| 84 |
config=_fast_fail_config(), |
| 85 |
) |
| 86 |
try: |
| 87 |
assert cache.set("check-connection-key", "value") |
| 88 |
assert cache.get("check-connection-key") == "value" |
| 89 |
finally: |
| 90 |
cache._table.delete() |