summaryrefslogtreecommitdiffstats
path: root/tests/unit/test_pastedeploy.py
diff options
context:
space:
mode:
authorMark McLoughlin <markmc@redhat.com>2012-03-16 17:03:56 -0400
committerMark McLoughlin <markmc@redhat.com>2012-03-26 07:16:16 +0100
commitbea9d49615563f73fe8be6ef5e71f96cc8fc1be3 (patch)
treebcc24592446368884d481713eefd07f3e7c8fb09 /tests/unit/test_pastedeploy.py
parentf9cb5279429921e20f0a55e9046f8a78649aa8ad (diff)
downloadoslo-bea9d49615563f73fe8be6ef5e71f96cc8fc1be3.tar.gz
oslo-bea9d49615563f73fe8be6ef5e71f96cc8fc1be3.tar.xz
oslo-bea9d49615563f73fe8be6ef5e71f96cc8fc1be3.zip
Add generic PasteDeploy app and filter factories
These generic factories allow us to dump copied and pasted app_factory and filter_factory methods. The main difference is the paste configuration changes from: [app:myapp] paste.app_factory = myapp:app_factory ... [filter:myfilter] paste.filter_factory = myapp:filter_factory to this: [app:myapp] paste.app_factory = openstack.common.pastedeploy:app_factory openstack.app_factory = myapp:App ... [filter:myfilter] paste.filter_factory = openstack.common.pastedeploy:filter_factory openstack.filter_factory = myapp:Filter Apart from reducing code duplication, this will also allow us to have the generic factories inject other data into the apps and filters. This could implemented as a new feature in PasteDeploy itself - e.g. allow the loadapp() caller supply a python object which is passed on to the factories. In the meantime, Glance has code like this to pass a ConfigOpts instance to factories. Keystone is moving a similar way, as will other projects as they move away from a global config object. Change-Id: I928d1f6da154f0f41edd624e25b8918a0e12cb28
Diffstat (limited to 'tests/unit/test_pastedeploy.py')
-rw-r--r--tests/unit/test_pastedeploy.py109
1 files changed, 109 insertions, 0 deletions
diff --git a/tests/unit/test_pastedeploy.py b/tests/unit/test_pastedeploy.py
new file mode 100644
index 0000000..9746fe5
--- /dev/null
+++ b/tests/unit/test_pastedeploy.py
@@ -0,0 +1,109 @@
+# vim: tabstop=4 shiftwidth=4 softtabstop=4
+
+# Copyright 2012 Red Hat, Inc.
+#
+# 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 os
+import tempfile
+import unittest
+
+from openstack.common import pastedeploy
+
+
+class App(object):
+
+ def __init__(self, data):
+ self.data = data
+
+
+class AppWithLocalConf(App):
+
+ def __init__(self, data, foo=None):
+ super(AppWithLocalConf, self).__init__(data)
+ self.foo = foo
+
+
+class Filter(object):
+
+ def __init__(self, app, data):
+ self.app = app
+ self.data = data
+
+
+class PasteTestCase(unittest.TestCase):
+
+ def setUp(self):
+ self.tempfiles = []
+
+ def tearDown(self):
+ self.remove_tempfiles()
+
+ def create_tempfile(self, contents):
+ (fd, path) = tempfile.mkstemp()
+ self.tempfiles.append(path)
+ try:
+ os.write(fd, contents)
+ finally:
+ os.close(fd)
+ return path
+
+ def remove_tempfiles(self):
+ for p in self.tempfiles:
+ os.remove(p)
+
+ def test_app_factory(self):
+ data = 'test_app_factory'
+
+ paste_conf = self.create_tempfile("""[DEFAULT]
+[app:myfoo]
+paste.app_factory = openstack.common.pastedeploy:app_factory
+openstack.app_factory = tests.unit.test_pastedeploy:App
+""")
+
+ app = pastedeploy.paste_deploy_app(paste_conf, 'myfoo', data)
+ self.assertEquals(app.data, data)
+
+ def test_app_factory_with_local_conf(self):
+ data = 'test_app_factory_with_local_conf'
+
+ paste_conf = self.create_tempfile("""[DEFAULT]
+[app:myfoo]
+paste.app_factory = openstack.common.pastedeploy:app_factory
+openstack.app_factory = tests.unit.test_pastedeploy:AppWithLocalConf
+foo = bar
+""")
+
+ app = pastedeploy.paste_deploy_app(paste_conf, 'myfoo', data)
+ self.assertEquals(app.data, data)
+ self.assertEquals(app.foo, 'bar')
+
+ def test_filter_factory(self):
+ data = 'test_filter_factory'
+
+ paste_conf = self.create_tempfile("""[DEFAULT]
+[pipeline:myfoo]
+pipeline = myfoofilter myfooapp
+
+[filter:myfoofilter]
+paste.filter_factory = openstack.common.pastedeploy:filter_factory
+openstack.filter_factory = tests.unit.test_pastedeploy:Filter
+
+[app:myfooapp]
+paste.app_factory = openstack.common.pastedeploy:app_factory
+openstack.app_factory = tests.unit.test_pastedeploy:App
+""")
+
+ app = pastedeploy.paste_deploy_app(paste_conf, 'myfoo', data)
+ self.assertEquals(app.data, data)
+ self.assertEquals(app.app.data, data)