Skip to content

Commit b8de326

Browse files
committed
Add TemporaryRegistry helper
1 parent b3db0e5 commit b8de326

File tree

2 files changed

+68
-15
lines changed

2 files changed

+68
-15
lines changed

src/aws_schema_registry/client.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
from datetime import datetime
12
import logging
23
import time
3-
from typing import Mapping
4+
import random
5+
import string
6+
from typing import ContextManager, Mapping
47
from uuid import UUID
58

69
from aws_schema_registry.schema import (
@@ -310,3 +313,57 @@ def create_schema(
310313
f'Create schema {name} failed'
311314
) from e
312315
return version_id
316+
317+
318+
class TemporaryRegistry(ContextManager):
319+
"""A real schema registry for use in tests and experiments.
320+
321+
This class implements the ContextManager protocol, creating the registry
322+
on enter and destroying it on exit.
323+
324+
Usage:
325+
326+
```python
327+
with TemporaryRegistry(glue_client, 'MyRegistry') as r:
328+
# registry is created on enter
329+
print(r.name) # the "real" (suffixed) registry name
330+
# registry is destroyed on exit
331+
```
332+
333+
Arguments:
334+
glue_client: glue client created by `botocore`/`boto3`.
335+
name: human-readable name for the created registry. The name will be
336+
suffixed by a random identifier to reduce the freqency of
337+
collisions.
338+
description: description for the created registry.
339+
autoremove: whether to destroy the created registry. Defaults to True.
340+
"""
341+
342+
DEFAULT_DESCRIPTION = 'Temporary registry created with the aws-glue-schema-registry Python library.' # NOQA
343+
344+
def __init__(self, glue_client,
345+
name: str = 'temporary-registry',
346+
description: str = DEFAULT_DESCRIPTION,
347+
autoremove: bool = True):
348+
self.glue_client = glue_client
349+
date = datetime.utcnow().strftime('%y-%m-%d-%H-%M')
350+
r = ''.join(random.choices(string.digits + string.ascii_letters,
351+
k=16))
352+
self.name = f'{name}-{date}-{r}'
353+
self.description = description
354+
self.autoremove = autoremove
355+
356+
def __enter__(self):
357+
LOG.info('creating registry %s...' % self.name)
358+
self.glue_client.create_registry(
359+
RegistryName=self.name,
360+
Description=self.description
361+
)
362+
return self
363+
364+
def __exit__(self, *args):
365+
if self.autoremove:
366+
LOG.info('deleting registry %s...' % self.name)
367+
self.glue_client.delete_registry(
368+
RegistryId={'RegistryName': self.name}
369+
)

tests/integration/conftest.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from datetime import datetime
21
import logging
32
import os
43

54
import boto3
65
import pytest
76

7+
from aws_schema_registry.client import TemporaryRegistry
8+
89
LOG = logging.getLogger(__name__)
910

1011
AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')
@@ -13,8 +14,6 @@
1314
AWS_REGION = os.getenv('AWS_REGION')
1415
AWS_PROFILE = os.getenv('AWS_PROFILE')
1516

16-
REGISTRY_PREFIX = 'schema-registry-tests'
17-
DATE = datetime.utcnow().strftime('%y-%m-%d-%H-%M-%s')
1817
CLEANUP_REGISTRY = os.getenv('CLEANUP_REGISTRY') is None
1918

2019

@@ -37,15 +36,12 @@ def glue_client(boto_session):
3736
@pytest.fixture(scope='session')
3837
def registry(glue_client):
3938
"""The AWS Glue registry to use for testing."""
40-
name = f'{REGISTRY_PREFIX}-{DATE}'
41-
LOG.info('creating registry %s...' % name)
42-
glue_client.create_registry(
43-
RegistryName=name,
44-
Description='Registry used for the schema registry python integration'
39+
with TemporaryRegistry(
40+
glue_client,
41+
name='schema-registry-tests',
42+
description='Registry used for the schema registry python integration'
4543
' tests. This registry does not hold any valuable data and is safe to'
46-
' delete as long as it is not currently in use by a test'
47-
)
48-
yield name
49-
if CLEANUP_REGISTRY:
50-
LOG.info('deleting registry %s...' % name)
51-
glue_client.delete_registry(RegistryId={'RegistryName': name})
44+
' delete as long as it is not currently in use by a test',
45+
autoremove=CLEANUP_REGISTRY
46+
) as registry:
47+
yield registry.name

0 commit comments

Comments
 (0)