Add basic unit test for the servicekeyworker
This commit is contained in:
parent
3b496e2759
commit
0afc222214
2 changed files with 26 additions and 3 deletions
|
@ -10,12 +10,12 @@ logger = logging.getLogger(__name__)
|
||||||
class ServiceKeyWorker(Worker):
|
class ServiceKeyWorker(Worker):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(ServiceKeyWorker, self).__init__()
|
super(ServiceKeyWorker, self).__init__()
|
||||||
self.add_operation(self._refresh_service_keys,
|
self.add_operation(self._refresh_service_key,
|
||||||
app.config.get('INSTANCE_SERVICE_KEY_REFRESH', 60)*60)
|
app.config.get('INSTANCE_SERVICE_KEY_REFRESH', 60)*60)
|
||||||
|
|
||||||
def _refresh_service_keys(self):
|
def _refresh_service_key(self):
|
||||||
"""
|
"""
|
||||||
Refreshes active service keys so they don't get garbage collected.
|
Refreshes the instance's active service key so it doesn't get garbage collected.
|
||||||
"""
|
"""
|
||||||
expiration = timedelta(minutes=instance_keys.service_key_expiration)
|
expiration = timedelta(minutes=instance_keys.service_key_expiration)
|
||||||
logger.debug('Starting refresh of automatic service keys')
|
logger.debug('Starting refresh of automatic service keys')
|
||||||
|
|
23
workers/servicekeyworker/test/test_servicekeyworker.py
Normal file
23
workers/servicekeyworker/test/test_servicekeyworker.py
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from mock import patch
|
||||||
|
|
||||||
|
from data import model
|
||||||
|
from workers.servicekeyworker.servicekeyworker import ServiceKeyWorker
|
||||||
|
from util.morecollections import AttrDict
|
||||||
|
|
||||||
|
from test.fixtures import *
|
||||||
|
|
||||||
|
def test_refresh_service_key(initialized_db):
|
||||||
|
# Create a service key for testing.
|
||||||
|
test_key = model.service_keys.create_service_key('test', 'somekid', 'quay', '', {},
|
||||||
|
datetime.now() + timedelta(minutes=10))
|
||||||
|
|
||||||
|
instance_keys = AttrDict(dict(local_key_id=test_key.kid, service_key_expiration=10))
|
||||||
|
|
||||||
|
with patch('workers.servicekeyworker.servicekeyworker.instance_keys', instance_keys):
|
||||||
|
worker = ServiceKeyWorker()
|
||||||
|
worker._refresh_service_key()
|
||||||
|
|
||||||
|
# Ensure the key's expiration was changed.
|
||||||
|
updated_key = model.service_keys.get_service_key(test_key.kid, approved_only=False)
|
||||||
|
assert updated_key.expiration_date > test_key.expiration_date
|
Reference in a new issue