summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorDolph Mathews <dolph.mathews@gmail.com>2012-12-19 10:04:21 -0600
committerDolph Mathews <dolph.mathews@gmail.com>2012-12-21 11:57:44 -0600
commit03eb2801a3ad38a39e9cf127c05ab710bf38ee1d (patch)
treec91ae80657c574cabcecf5abfa78f3940392c517 /tests
parentac2d92ca2eea1070f765be320acb62fd5bef6dd3 (diff)
downloadkeystone-03eb2801a3ad38a39e9cf127c05ab710bf38ee1d.tar.gz
keystone-03eb2801a3ad38a39e9cf127c05ab710bf38ee1d.tar.xz
keystone-03eb2801a3ad38a39e9cf127c05ab710bf38ee1d.zip
Driver registry
Uses automatic dependency injection to provide controllers with driver interfaces (identity_api, token_api, etc). See tests/test_injection.py for a self-contained example. Change-Id: I255087de534292fbf57a45b19f97488f831f607c
Diffstat (limited to 'tests')
-rw-r--r--tests/test_auth.py14
-rw-r--r--tests/test_injection.py141
2 files changed, 144 insertions, 11 deletions
diff --git a/tests/test_auth.py b/tests/test_auth.py
index c0dbbd2c..75f06174 100644
--- a/tests/test_auth.py
+++ b/tests/test_auth.py
@@ -17,13 +17,9 @@ import uuid
import default_fixtures
-from keystone import catalog
from keystone import config
from keystone import exception
-from keystone import identity
-from keystone.identity.backends import kvs as kvs_identity
from keystone.openstack.common import timeutils
-from keystone import policy
from keystone import test
from keystone import token
@@ -56,15 +52,11 @@ class AuthTest(test.TestCase):
def setUp(self):
super(AuthTest, self).setUp()
- # load_fixtures checks for 'identity_api' to be defined
- self.identity_api = kvs_identity.Identity()
+ CONF.identity.driver = 'keystone.identity.backends.kvs.Identity'
+ self.load_backends()
self.load_fixtures(default_fixtures)
- self.api = token.controllers.Auth(
- catalog_api=catalog.Manager(),
- identity_api=identity.Manager(),
- policy_api=policy.Manager(),
- token_api=token.Manager())
+ self.api = token.controllers.Auth()
def assertEqualTokens(self, a, b):
"""Assert that two tokens are equal.
diff --git a/tests/test_injection.py b/tests/test_injection.py
new file mode 100644
index 00000000..4953cfca
--- /dev/null
+++ b/tests/test_injection.py
@@ -0,0 +1,141 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2012 OpenStack LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import unittest2 as unittest
+import uuid
+
+from keystone.common import dependency
+
+
+class TestDependencyInjection(unittest.TestCase):
+ def test_dependency_injection(self):
+ class Interface(object):
+ def do_work(self):
+ assert False
+
+ @dependency.provider('first_api')
+ class FirstImplementation(Interface):
+ def do_work(self):
+ return True
+
+ @dependency.provider('second_api')
+ class SecondImplementation(Interface):
+ def do_work(self):
+ return True
+
+ @dependency.requires('first_api', 'second_api')
+ class Consumer(object):
+ def do_work_with_dependencies(self):
+ assert self.first_api.do_work()
+ assert self.second_api.do_work()
+
+ # initialize dependency providers
+ first_api = FirstImplementation()
+ second_api = SecondImplementation()
+
+ # ... sometime later, initialize a dependency consumer
+ consumer = Consumer()
+
+ # the expected dependencies should be available to the consumer
+ self.assertIs(consumer.first_api, first_api)
+ self.assertIs(consumer.second_api, second_api)
+ self.assertIsInstance(consumer.first_api, Interface)
+ self.assertIsInstance(consumer.second_api, Interface)
+ consumer.do_work_with_dependencies()
+
+ def test_dependency_configuration(self):
+ @dependency.provider('api')
+ class Configurable(object):
+ def __init__(self, value=None):
+ self.value = value
+
+ def get_value(self):
+ return self.value
+
+ @dependency.requires('api')
+ class Consumer(object):
+ def get_value(self):
+ return self.api.get_value()
+
+ # initialize dependency providers
+ api = Configurable(value=True)
+
+ # ... sometime later, initialize a dependency consumer
+ consumer = Consumer()
+
+ # the expected dependencies should be available to the consumer
+ self.assertIs(consumer.api, api)
+ self.assertIsInstance(consumer.api, Configurable)
+ self.assertTrue(consumer.get_value())
+
+ def test_inherited_dependency(self):
+ class Interface(object):
+ def do_work(self):
+ assert False
+
+ @dependency.provider('first_api')
+ class FirstImplementation(Interface):
+ def do_work(self):
+ return True
+
+ @dependency.provider('second_api')
+ class SecondImplementation(Interface):
+ def do_work(self):
+ return True
+
+ @dependency.requires('first_api')
+ class ParentConsumer(object):
+ def do_work_with_dependencies(self):
+ assert self.first_api.do_work()
+
+ @dependency.requires('second_api')
+ class ChildConsumer(ParentConsumer):
+ def do_work_with_dependencies(self):
+ assert self.second_api.do_work()
+ super(ChildConsumer, self).do_work_with_dependencies()
+
+ # initialize dependency providers
+ first_api = FirstImplementation()
+ second_api = SecondImplementation()
+
+ # ... sometime later, initialize a dependency consumer
+ consumer = ChildConsumer()
+
+ # dependencies should be naturally inherited
+ self.assertEqual(
+ ParentConsumer._dependencies,
+ set(['first_api']))
+ self.assertEqual(
+ ChildConsumer._dependencies,
+ set(['first_api', 'second_api']))
+ self.assertEqual(
+ consumer._dependencies,
+ set(['first_api', 'second_api']))
+
+ # the expected dependencies should be available to the consumer
+ self.assertIs(consumer.first_api, first_api)
+ self.assertIs(consumer.second_api, second_api)
+ self.assertIsInstance(consumer.first_api, Interface)
+ self.assertIsInstance(consumer.second_api, Interface)
+ consumer.do_work_with_dependencies()
+
+ def test_unresolvable_dependency(self):
+ @dependency.requires(uuid.uuid4().hex)
+ class Consumer(object):
+ pass
+
+ with self.assertRaises(dependency.UnresolvableDependencyException):
+ Consumer()