Commit 3eb0469d authored by Hanno Schlichting's avatar Hanno Schlichting

Split zope.conf dependent tests into WSGI/non-WSGI classes.

parent 70f7997f
......@@ -39,7 +39,7 @@ instancehome <<INSTANCE_HOME>>
def getSchema():
startup = os.path.dirname(os.path.realpath(Zope2.Startup.__file__))
schemafile = os.path.join(startup, 'zopeschema.xml')
schemafile = os.path.join(startup, 'wsgischema.xml')
return ZConfig.loadSchema(schemafile)
......
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
import os, unittest, tempfile, shutil, cStringIO
from OFS.Application import Application
import Zope2.Startup
import ZConfig
from App.config import getConfiguration, setConfiguration
import Products
TEMPNAME = tempfile.mktemp()
TEMPPRODUCTS = os.path.join(TEMPNAME, "Products")
TEMPPRODUCTS2 = os.path.join(TEMPNAME, "Products2")
FAKEPRODUCTS = ['foo', 'bar', 'bee', 'baz']
cfg = """
instancehome <<INSTANCE_HOME>>
products <<PRODUCTS>>
products <<PRODUCTS2>>
<zodb_db main>
mount-point /
<mappingstorage>
name mappingstorage
</mappingstorage>
</zodb_db>
"""
# CAUTION: A raw string must be used in the open() call. This is crucial
# so that if you happen to be on Windows, and are passed a path containing
# backslashes, the backslashes get treated *as* backslashes instead of as
# string escape codes.
dummy_product_init = """
misc_ = {'a':1}
def amethod(self):
pass
def initialize(context):
f=open(r'%s', 'w')
f.write('didit')
f.close()
context.registerClass(
meta_type='grabass',
permission='aPermission',
constructors=(amethod,),
legacy=(amethod,))
"""
def getSchema():
startup = os.path.dirname(os.path.realpath(Zope2.Startup.__file__))
schemafile = os.path.join(startup, 'zopeschema.xml')
return ZConfig.loadSchema(schemafile)
def getApp():
from App.ZApplication import ZApplicationWrapper
DB = getConfiguration().dbtab.getDatabase('/')
return ZApplicationWrapper(DB, 'Application', Application, ())()
original_config = None
class TestProductInit( unittest.TestCase ):
""" Test the application initializer object """
def setUp(self):
global original_config
if original_config is None:
original_config = getConfiguration()
self.schema = getSchema()
os.makedirs(TEMPNAME)
os.makedirs(TEMPPRODUCTS)
os.makedirs(TEMPPRODUCTS2)
def tearDown(self):
import App.config
del self.schema
App.config.setConfiguration(original_config)
shutil.rmtree(TEMPNAME)
Products.__path__ = [d for d in Products.__path__
if os.path.exists(d)]
def configure(self, text):
# We have to create a directory of our own since the existence
# of the directory is checked. This handles this in a
# platform-independent way.
schema = self.schema
text = text.replace("<<INSTANCE_HOME>>", TEMPNAME)
text = text.replace("<<PRODUCTS>>", TEMPPRODUCTS)
text = text.replace("<<PRODUCTS2>>", TEMPPRODUCTS2)
sio = cStringIO.StringIO(text)
conf, handler = ZConfig.loadConfigFile(schema, sio)
from Zope2.Startup.handlers import handleConfig
handleConfig(conf, handler)
self.assertEqual(conf.instancehome, TEMPNAME)
setConfiguration(conf)
def makeProduct(self, proddir):
os.makedirs(proddir)
f = open(os.path.join(proddir, '__init__.py'), 'w')
f.write('#foo')
f.close()
def makeFakeProducts(self):
for name in FAKEPRODUCTS:
proddir = os.path.join(TEMPPRODUCTS, name)
self.makeProduct(proddir)
def test_get_products(self):
self.makeFakeProducts()
self.configure(cfg)
from OFS.Application import get_products
names = [x[1] for x in get_products()]
for name in FAKEPRODUCTS:
self.assert_(name in names)
def test_empty_dir_on_products_path_is_not_product(self):
self.makeFakeProducts()
os.makedirs(os.path.join(TEMPPRODUCTS, 'gleeb'))
self.configure(cfg)
from OFS.Application import get_products
names = [x[1] for x in get_products()]
for name in FAKEPRODUCTS:
self.assert_(name in names)
self.assert_('gleeb' not in names)
def test_file_on_products_path_is_not_product(self):
self.makeFakeProducts()
f = open(os.path.join(TEMPPRODUCTS, 'README.txt'), 'w')
f.write('#foo')
f.close()
self.configure(cfg)
from OFS.Application import get_products
names = [x[1] for x in get_products()]
for name in FAKEPRODUCTS:
self.assert_(name in names)
self.assert_('README.txt' not in names)
def test_multiple_product_paths(self):
self.makeFakeProducts()
self.makeProduct(os.path.join(TEMPPRODUCTS2, 'another'))
self.configure(cfg)
from OFS.Application import get_products
names = [x[1] for x in get_products()]
for name in FAKEPRODUCTS:
self.assert_(name in names)
self.assert_('another' in names)
def test_import_products(self):
self.makeFakeProducts()
self.configure(cfg)
from OFS.Application import import_products
names = import_products()
for name in FAKEPRODUCTS:
assert name in names
def test_import_product_throws(self):
self.makeProduct(os.path.join(TEMPPRODUCTS, 'abar'))
f = open(os.path.join(TEMPPRODUCTS, 'abar', '__init__.py'), 'w')
f.write('Syntax Error!')
f.close()
self.configure(cfg)
try:
from logging import getLogger
logger = getLogger('Zope')
logger.disabled = 1
self.assertRaises(SyntaxError, self.import_bad_product)
finally:
logger.disabled = 0
def import_bad_product(self):
from OFS.Application import import_product
import_product(TEMPPRODUCTS, 'abar', raise_exc=1)
def test_install_product(self):
self.makeProduct(os.path.join(TEMPPRODUCTS, 'abaz'))
f = open(os.path.join(TEMPPRODUCTS, 'abaz', '__init__.py'), 'w')
doneflag = os.path.join(TEMPPRODUCTS, 'abaz', 'doneflag')
f.write(dummy_product_init % doneflag)
f.close()
self.configure(cfg)
from OFS.Application import install_product, get_folder_permissions,\
Application
import Products
from OFS.Folder import Folder
app = getApp()
meta_types = []
install_product(app, TEMPPRODUCTS, 'abaz', meta_types,
get_folder_permissions(), raise_exc=1)
# misc_ dictionary is updated
self.assert_(Application.misc_.__dict__.has_key('abaz'))
# initialize is called
self.assert_(os.path.exists(doneflag))
# Methods installed into folder
self.assert_(hasattr(Folder, 'amethod'))
# permission roles put into folder
self.assert_(hasattr(Folder, 'amethod__roles__'))
# Products.meta_types updated
self.assert_( {'name': 'grabass',
'action': 'manage_addProduct/abaz/amethod',
'product': 'abaz',
'permission': 'aPermission',
'visibility': 'Global',
'interfaces': (),
'instance': None,
'container_filter': None}
in Products.meta_types)
def test_suite():
suite = unittest.TestSuite()
suite.addTest( unittest.makeSuite( TestProductInit ) )
return suite
......@@ -23,17 +23,16 @@ import tempfile
import unittest
import ZConfig
from ZConfig.components.logger.tests.test_logger import LoggingTestHelper
import Zope2.Startup
import Products
from App.config import getConfiguration, setConfiguration
import Products
from Zope2.Startup import get_starter
from Zope2.Startup.options import ZopeOptions
TEMPNAME = tempfile.mktemp()
TEMPPRODUCTS = os.path.join(TEMPNAME, "Products")
_SCHEMA = None
_SCHEMA = {}
LIFETIME = True
try:
......@@ -42,13 +41,14 @@ except ImportError:
LIFETIME = False
def getSchema():
def getSchema(schemafile):
global _SCHEMA
if _SCHEMA is None:
startup = os.path.dirname(Zope2.Startup.__file__)
schemafile = os.path.join(startup, 'zopeschema.xml')
_SCHEMA = ZConfig.loadSchema(schemafile)
return _SCHEMA
if schemafile not in _SCHEMA:
opts = ZopeOptions()
opts.schemafile = schemafile
opts.load_schema()
_SCHEMA[schemafile] = opts.schema
return _SCHEMA[schemafile]
# try to preserve logging state so we don't screw up other unit tests
# that come later
......@@ -83,16 +83,12 @@ class BaseTestCase(LoggingTestHelper):
logger = logging.getLogger(name)
logger.__dict__.update(logger_states[name])
@property
def schema(self):
return getSchema()
def _clearHandlers(self):
from ZConfig.components.logger import loghandler
del loghandler._reopenable_handlers[:]
def get_starter(self, conf, wsgi=False):
starter = Zope2.Startup.get_starter(wsgi=wsgi)
starter = get_starter(wsgi=wsgi)
starter.setConfiguration(conf)
return starter
......@@ -117,6 +113,10 @@ class BaseTestCase(LoggingTestHelper):
class WSGIStarterTestCase(BaseTestCase, unittest.TestCase):
@property
def schema(self):
return getSchema('wsgischema.xml')
def testSetupLocale(self):
# XXX this almost certainly won't work on all systems
import locale
......@@ -247,6 +247,10 @@ class WSGIStarterTestCase(BaseTestCase, unittest.TestCase):
if LIFETIME:
class ZopeStarterTestCase(BaseTestCase, unittest.TestCase):
@property
def schema(self):
return getSchema('zopeschema.xml')
def testDropPrivileges(self):
# somewhat incomplete because we we're never running as root
# when we test, but we test as much as we can
......
......@@ -19,34 +19,32 @@ import tempfile
import unittest
import ZConfig
import Zope2.Startup
import Products
import Products
from Zope2.Startup import datatypes
from Zope2.Startup.options import ZopeOptions
_SCHEMA = {}
TEMPNAME = tempfile.mktemp()
TEMPPRODUCTS = os.path.join(TEMPNAME, "Products")
TEMPVAR = os.path.join(TEMPNAME, "var")
def getSchema():
startup = os.path.dirname(os.path.realpath(Zope2.Startup.__file__))
schemafile = os.path.join(startup, 'zopeschema.xml')
return ZConfig.loadSchema(schemafile)
def getSchema(schemafile):
global _SCHEMA
if schemafile not in _SCHEMA:
opts = ZopeOptions()
opts.schemafile = schemafile
opts.load_schema()
_SCHEMA[schemafile] = opts.schema
return _SCHEMA[schemafile]
class StartupTestCase(unittest.TestCase):
class WSGIStartupTestCase(unittest.TestCase):
schema = None
def setUp(self):
if self.schema is None:
StartupTestCase.schema = getSchema()
def tearDown(self):
Products.__path__ = [d for d in Products.__path__
if os.path.exists(d)]
@property
def schema(self):
return getSchema('wsgischema.xml')
def load_config_text(self, text):
# We have to create a directory of our own since the existence
......@@ -56,12 +54,10 @@ class StartupTestCase(unittest.TestCase):
sio = cStringIO.StringIO(
text.replace("<<INSTANCE_HOME>>", TEMPNAME))
os.mkdir(TEMPNAME)
os.mkdir(TEMPPRODUCTS)
os.mkdir(TEMPVAR)
try:
conf, handler = ZConfig.loadConfigFile(schema, sio)
finally:
os.rmdir(TEMPPRODUCTS)
os.rmdir(TEMPVAR)
os.rmdir(TEMPNAME)
self.assertEqual(conf.instancehome, TEMPNAME)
......@@ -76,19 +72,6 @@ class StartupTestCase(unittest.TestCase):
f.close()
self.load_config_text(text)
def test_cgi_environment(self):
conf, handler = self.load_config_text("""\
# instancehome is here since it's required
instancehome <<INSTANCE_HOME>>
<cgi-environment>
HEADER value
ANOTHER value2
</cgi-environment>
""")
items = conf.cgi_environment.items()
items.sort()
self.assertEqual(items, [("ANOTHER", "value2"), ("HEADER", "value")])
def test_environment(self):
conf, handler = self.load_config_text("""\
# instancehome is here since it's required
......@@ -103,6 +86,88 @@ class StartupTestCase(unittest.TestCase):
self.assertEqual(
items, [("FEARFACTORY", "rocks"), ("NSYNC", "doesnt")])
def test_zodb_db(self):
conf, handler = self.load_config_text("""\
instancehome <<INSTANCE_HOME>>
<zodb_db main>
<filestorage>
path <<INSTANCE_HOME>>/var/Data.fs
</filestorage>
mount-point /
cache-size 5000
pool-size 7
</zodb_db>
""")
self.assertEqual(conf.databases[0].config.cache_size, 5000)
def test_max_conflict_retries_default(self):
conf, handler = self.load_config_text("""\
instancehome <<INSTANCE_HOME>>
""")
self.assertEqual(conf.max_conflict_retries, 3)
def test_max_conflict_retries_explicit(self):
conf, handler = self.load_config_text("""\
instancehome <<INSTANCE_HOME>>
max-conflict-retries 15
""")
self.assertEqual(conf.max_conflict_retries, 15)
def test_default_zpublisher_encoding(self):
conf, dummy = self.load_config_text("""\
instancehome <<INSTANCE_HOME>>
""")
self.assertEqual(conf.default_zpublisher_encoding, 'utf-8')
conf, dummy = self.load_config_text("""\
instancehome <<INSTANCE_HOME>>
default-zpublisher-encoding iso-8859-15
""")
self.assertEqual(conf.default_zpublisher_encoding, 'iso-8859-15')
class ZServerStartupTestCase(unittest.TestCase):
def tearDown(self):
Products.__path__ = [d for d in Products.__path__
if os.path.exists(d)]
@property
def schema(self):
return getSchema('zopeschema.xml')
def load_config_text(self, text):
# We have to create a directory of our own since the existence
# of the directory is checked. This handles this in a
# platform-independent way.
schema = self.schema
sio = cStringIO.StringIO(
text.replace("<<INSTANCE_HOME>>", TEMPNAME))
os.mkdir(TEMPNAME)
os.mkdir(TEMPPRODUCTS)
os.mkdir(TEMPVAR)
try:
conf, handler = ZConfig.loadConfigFile(schema, sio)
finally:
os.rmdir(TEMPPRODUCTS)
os.rmdir(TEMPVAR)
os.rmdir(TEMPNAME)
self.assertEqual(conf.instancehome, TEMPNAME)
return conf, handler
def test_cgi_environment(self):
conf, handler = self.load_config_text("""\
# instancehome is here since it's required
instancehome <<INSTANCE_HOME>>
<cgi-environment>
HEADER value
ANOTHER value2
</cgi-environment>
""")
items = conf.cgi_environment.items()
items.sort()
self.assertEqual(items, [("ANOTHER", "value2"), ("HEADER", "value")])
def test_ms_public_header(self):
from Zope2.Startup import config
from Zope2.Startup.handlers import handleConfig
......@@ -159,50 +224,3 @@ class StartupTestCase(unittest.TestCase):
self.assertEqual(conf.access.name, "access")
self.assertEqual(conf.access.handler_factories[0].section.path, fn)
self.assert_(conf.trace is None)
def test_dns_resolver(self):
from ZServer.medusa import resolver
conf, handler = self.load_config_text("""\
instancehome <<INSTANCE_HOME>>
dns-server localhost
""")
self.assert_(isinstance(conf.dns_resolver, resolver.caching_resolver))
def test_zodb_db(self):
conf, handler = self.load_config_text("""\
instancehome <<INSTANCE_HOME>>
<zodb_db main>
<filestorage>
path <<INSTANCE_HOME>>/var/Data.fs
</filestorage>
mount-point /
cache-size 5000
pool-size 7
</zodb_db>
""")
self.assertEqual(conf.databases[0].config.cache_size, 5000)
def test_max_conflict_retries_default(self):
conf, handler = self.load_config_text("""\
instancehome <<INSTANCE_HOME>>
""")
self.assertEqual(conf.max_conflict_retries, 3)
def test_max_conflict_retries_explicit(self):
conf, handler = self.load_config_text("""\
instancehome <<INSTANCE_HOME>>
max-conflict-retries 15
""")
self.assertEqual(conf.max_conflict_retries, 15)
def test_default_zpublisher_encoding(self):
conf, dummy = self.load_config_text("""\
instancehome <<INSTANCE_HOME>>
""")
self.assertEqual(conf.default_zpublisher_encoding, 'utf-8')
conf, dummy = self.load_config_text("""\
instancehome <<INSTANCE_HOME>>
default-zpublisher-encoding iso-8859-15
""")
self.assertEqual(conf.default_zpublisher_encoding, 'iso-8859-15')
......@@ -30,7 +30,7 @@ TEMPPRODUCTS = os.path.join(TEMPNAME, "Products")
def getSchema():
startup = os.path.dirname(os.path.realpath(Zope2.Startup.__file__))
schemafile = os.path.join(startup, 'zopeschema.xml')
schemafile = os.path.join(startup, 'wsgischema.xml')
return ZConfig.loadSchema(schemafile)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment