From 932db23a5ca00206945908398140f586baecb9ce Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Wed, 12 Jul 2017 16:37:32 +0300 Subject: [PATCH] Change servicekeyworker to use a data interface --- workers/servicekeyworker/models_interface.py | 27 +++++++++++++++++++ workers/servicekeyworker/models_pre_oci.py | 19 +++++++++++++ workers/servicekeyworker/servicekeyworker.py | 4 +-- .../test/test_servicekeyworker.py | 11 ++++---- 4 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 workers/servicekeyworker/models_interface.py create mode 100644 workers/servicekeyworker/models_pre_oci.py diff --git a/workers/servicekeyworker/models_interface.py b/workers/servicekeyworker/models_interface.py new file mode 100644 index 000000000..6b9292b91 --- /dev/null +++ b/workers/servicekeyworker/models_interface.py @@ -0,0 +1,27 @@ +from abc import ABCMeta, abstractmethod +from six import add_metaclass + +@add_metaclass(ABCMeta) +class ServiceKeyWorkerDataInterface(object): + """ + Interface that represents all data store interactions required by the service key worker. + """ + + @abstractmethod + def set_key_expiration(self, key_id, expiration_date): + """ Sets the expiration date of the service key with the given key ID to that given. """ + pass + + @abstractmethod + def create_service_key_for_testing(self, expiration): + """ Creates a service key for testing with the given expiration. Returns the KID for + key. + """ + pass + + @abstractmethod + def get_service_key_expiration(self, key_id): + """ Returns the expiration date for the key with the given ID. If the key doesn't exist or + does not have an expiration, returns None. + """ + pass diff --git a/workers/servicekeyworker/models_pre_oci.py b/workers/servicekeyworker/models_pre_oci.py new file mode 100644 index 000000000..906572397 --- /dev/null +++ b/workers/servicekeyworker/models_pre_oci.py @@ -0,0 +1,19 @@ +from data import model +from workers.servicekeyworker.models_interface import ServiceKeyWorkerDataInterface + +class PreOCIModel(ServiceKeyWorkerDataInterface): + def set_key_expiration(self, kid, expiration_date): + model.service_keys.set_key_expiration(kid, expiration_date) + + def create_service_key_for_testing(self, expiration): + key = model.service_keys.create_service_key('test', 'somekid', 'quay', '', {}, expiration) + return key.kid + + def get_service_key_expiration(self, kid): + try: + key = model.service_keys.get_service_key(kid, approved_only=False) + return key.expiration_date + except model.ServiceKeyDoesNotExist: + return None + +pre_oci_model = PreOCIModel() diff --git a/workers/servicekeyworker/servicekeyworker.py b/workers/servicekeyworker/servicekeyworker.py index 8539699a0..90ddd0e33 100644 --- a/workers/servicekeyworker/servicekeyworker.py +++ b/workers/servicekeyworker/servicekeyworker.py @@ -2,7 +2,7 @@ import logging from datetime import datetime, timedelta from app import app, instance_keys -from data.model.service_keys import set_key_expiration +from workers.servicekeyworker.models_pre_oci import pre_oci_model as model from workers.worker import Worker logger = logging.getLogger(__name__) @@ -19,7 +19,7 @@ class ServiceKeyWorker(Worker): """ expiration = timedelta(minutes=instance_keys.service_key_expiration) logger.debug('Starting refresh of automatic service keys') - set_key_expiration(instance_keys.local_key_id, datetime.now() + expiration) + model.set_key_expiration(instance_keys.local_key_id, datetime.now() + expiration) logger.debug('Finished refresh of automatic service keys') if __name__ == "__main__": diff --git a/workers/servicekeyworker/test/test_servicekeyworker.py b/workers/servicekeyworker/test/test_servicekeyworker.py index 40ddcf139..7db38c62b 100644 --- a/workers/servicekeyworker/test/test_servicekeyworker.py +++ b/workers/servicekeyworker/test/test_servicekeyworker.py @@ -6,18 +6,17 @@ from workers.servicekeyworker.servicekeyworker import ServiceKeyWorker from util.morecollections import AttrDict from test.fixtures import * +from workers.servicekeyworker.models_pre_oci import pre_oci_model as model 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)) + original_expiration = datetime.now() + timedelta(minutes=10) + test_key_kid = model.create_service_key_for_testing(original_expiration) + 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 + assert model.get_service_key_expiration(test_key_kid) > original_expiration