Exemple #1
0
    def test_import_errors(self):
        # must specify exactly one of package or src
        self.assertRaises(ZConfig.SchemaError, ZConfig.loadSchemaFile,
                          StringIO("<schema><import/></schema>"))
        self.assertRaises(ZConfig.SchemaError, ZConfig.loadSchemaFile,
                          StringIO("<schema>"
                                   "  <import src='library.xml'"
                                   "          package='ZConfig'/>"
                                   "</schema>"))
        # cannot specify src and file
        self.assertRaises(ZConfig.SchemaError, ZConfig.loadSchemaFile,
                          StringIO("<schema>"
                                   "  <import src='library.xml'"
                                   "          file='other.xml'/>"
                                   "</schema>"))
        # cannot specify module as package
        sio = StringIO("<schema>"
                       "  <import package='ZConfig.tests.test_loader'/>"
                       "</schema>")
        with self.assertRaises(ZConfig.SchemaResourceError) as ctx:
            ZConfig.loadSchemaFile(sio)

        e = ctx.exception
        self.assertEqual(e.filename, "component.xml")
        self.assertEqual(e.package, "ZConfig.tests.test_loader")
        self.assertTrue(e.path is None)
        # make sure the str() doesn't raise an unexpected exception
        str(e)
Exemple #2
0
def run_with_options(options):
    conf_fn = options.config_file

    # Do the gevent stuff ASAP
    if getattr(options, 'gevent', False):
        import gevent.monkey
        gevent.monkey.patch_all()

    if options.log:
        import logging
        lvl_map = getattr(logging, '_nameToLevel', None) or getattr(logging, '_levelNames', {})
        logging.basicConfig(level=lvl_map.get(options.log, logging.INFO),
                            format='%(asctime)s %(levelname)-5.5s [%(name)s][%(thread)d:%(process)d][%(threadName)s] %(message)s')

    object_size = max(options.object_size, pobject_base_size)
    if options.profile_dir and not os.path.exists(options.profile_dir):
        os.makedirs(options.profile_dir)

    schema = ZConfig.loadSchemaFile(StringIO(schema_xml))
    config, _handler = ZConfig.loadConfigFile(schema, conf_fn)
    contenders = [(db.name, db) for db in config.databases]

    if options.zap:
        _zap(contenders)

    # results: {contender_name: {concurrency_level: {objects_per_txn: [[SpeedTestTimes]...]}}}
    results = defaultdict(lambda: defaultdict(dict))

    try:
        for objects_per_txn in options.counts or DEFAULT_OBJ_COUNTS:
            for concurrency in options.concurrency or DEFAULT_CONCURRENCIES:
                speedtest = SpeedTest(
                    concurrency, objects_per_txn,
                    object_size,
                    options.profile_dir,
                    'threads' if options.threads else 'mp',
                    test_reps=options.test_reps)
                if options.btrees:
                    import BTrees
                    if options.btrees == 'IO':
                        speedtest.MappingType = BTrees.family64.IO.BTree
                    else:
                        speedtest.MappingType = BTrees.family64.OO.BTree

                for contender_name, db in contenders:
                    print((
                        'Testing %s with objects_per_txn=%d, object_size=%d, '
                        'mappingtype=%s and concurrency=%d (threads? %s)'
                        % (contender_name, objects_per_txn, object_size,
                           speedtest.MappingType,
                           concurrency, options.threads)), file=sys.stderr)

                    all_times = _run_one_contender(options, speedtest, contender_name, db)
                    #results[key] = all_times
                    results[contender_name][concurrency][objects_per_txn] = all_times

    # The finally clause causes test results to print even if the tests
    # stop early.
    finally:
        _print_results(options, contenders, results)
Exemple #3
0
    def test_zip_import_component_from_config(self):
        sio = StringIO('''
            <schema>
              <abstracttype name="something"/>
              <section name="*"
                       attribute="something"
                       type="something"
                       />
            </schema>
            ''')
        schema = ZConfig.loadSchemaFile(sio)

        value = '''
            %import foo.sample
            <sample>
              data value
            </sample>
            '''
        sio = StringIO(value)
        config, _ = ZConfig.loadConfigFile(schema, sio)
        self.assertEqual(config.something.data, "| value |")

        sio = StringIO(value)
        with self.assertRaises(ZConfig.ConfigurationSyntaxError):
            ZConfig.loadConfigFile(schema, sio,
                                   overrides=["sample/data=othervalue"])
Exemple #4
0
    def checkConfigureViaZConfig(self):
        replica_conf = os.path.join(os.path.dirname(__file__), 'replicas.conf')
        if self.keep_history:
            dbname = base_dbname
        else:
            dbname = base_dbname + '_hf'
        conf = u"""
        %%import relstorage
        <zodb main>
            <relstorage>
            name xyz
            read-only false
            keep-history %s
            replica-conf %s
            blob-chunk-size 10MB
            <mysql>
                driver auto
                db %s
                user relstoragetest
                passwd relstoragetest
            </mysql>
            </relstorage>
        </zodb>
        """ % (
            self.keep_history and 'true' or 'false',
            replica_conf,
            dbname,
            )

        schema_xml = u"""
        <schema>
        <import package="ZODB"/>
        <section type="ZODB.database" name="main" attribute="database"/>
        </schema>
        """
        import ZConfig
        from io import StringIO
        schema = ZConfig.loadSchemaFile(StringIO(schema_xml))
        config, _ = ZConfig.loadConfigFile(schema, StringIO(conf))

        db = config.database.open()
        try:
            storage = db.storage
            self.assertEqual(storage.isReadOnly(), False)
            self.assertEqual(storage.getName(), "xyz")
            adapter = storage._adapter
            from relstorage.adapters.mysql import MySQLAdapter
            self.assertIsInstance(adapter, MySQLAdapter)
            self.assertEqual(adapter._params, {
                'passwd': 'relstoragetest',
                'db': dbname,
                'user': '******',
                })
            self.assertEqual(adapter.keep_history, self.keep_history)
            self.assertEqual(
                adapter.connmanager.replica_selector.replica_conf,
                replica_conf)
            self.assertEqual(storage._options.blob_chunk_size, 10485760)
        finally:
            db.close()
Exemple #5
0
    def __call__(self, uri):
        (scheme, netloc, path, query, frag) = urlparse.urlsplit(uri)
        # urlparse doesnt understand file URLs and stuffs everything into path
        (scheme, netloc, path, query, frag) = urlparse.urlsplit("http:" + path)
        path = os.path.normpath(path)
        schema_xml = self.schema_xml_template
        schema = ZConfig.loadSchemaFile(StringIO(schema_xml))
        config, handler = ZConfig.loadConfig(schema, path)
        for config_item in config.databases + config.storages:
            if not frag:
                # use the first defined in the file
                break
            elif frag == config_item.name:
                # match found
                break
        else:
            raise KeyError("No storage or database named %s found" % frag)

        if isinstance(config_item, ZODBDatabase):
            config = config_item.config
            factory = config.storage
            dbkw = {"connection_cache_size": config.cache_size, "connection_pool_size": config.pool_size}
            if config.database_name:
                dbkw["database_name"] = config.database_name
        else:
            factory = config_item
            dbkw = dict(cgi.parse_qsl(query))

        return factory.open, dbkw
Exemple #6
0
def worker(app, global_conf, zookeeper, path, loggers=None, address=':0',
           threads=None, backdoor=False, description=None, version=None,
           run=True, **kw):
    """Paste deploy server runner
    """
    if loggers:
        if re.match(r'\d+$', loggers):
            logging.basicConfig(level=int(loggers))
        elif loggers in ('CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'):
            logging.basicConfig(level=getattr(logging, loggers))
        else:
            import ZConfig
            ZConfig.configureLoggers(loggers)

    zk = zc.zk.ZooKeeper(zookeeper)
    address = zc.parse_addr.parse_addr(address)
    from zc.resumelb.worker import Worker

    worker = Worker(app, address, threads=threads and int(threads),
                    **kw)

    # Set up notification of settings changes.
    settings = zk.properties(path)
    watcher = gevent.get_hub().loop.async()
    watcher.start(lambda : worker.update_settings(settings))
    settings(lambda _: watcher.send())

    registration_data = {}
    if backdoor == 'true':
        from gevent import backdoor
        bd = backdoor.BackdoorServer(('127.0.0.1', 0), locals())
        bd.start()
        registration_data['backdoor'] = '127.0.0.1:%s' % bd.server_port
        worker.__bd = bd

    if description:
        registration_data['description'] = description

    if version:
        registration_data['version'] = version

    zk.register_server(path+'/providers', worker.addr, **registration_data)
    worker.zk = zk
    worker.__zksettings = settings

    def shutdown():
        zk.close()
        worker.shutdown()

    gevent.signal(signal.SIGTERM, shutdown)

    if run:
        try:
            worker.server.serve_forever()
        finally:
            logging.getLogger(__name__+'.worker').info('exiting')
            zk.close()
    else:
        gevent.sleep(.01)
        return worker
Exemple #7
0
    def checkConfigureViaZConfig(self):
        replica_conf = os.path.join(os.path.dirname(__file__), 'replicas.conf')
        if self.keep_history:
            dbname = base_dbname
        else:
            dbname = base_dbname + '_hf'
        dsn = (
            "dbname='%s' user='******' password='******'"
            % dbname)
        conf = """
        %%import relstorage
        <zodb main>
            <relstorage>
            name xyz
            read-only false
            keep-history %s
            replica-conf %s
            blob-chunk-size 10MB
            <postgresql>
                dsn %s
            </postgresql>
            </relstorage>
        </zodb>
        """ % (
            self.keep_history and 'true' or 'false',
            replica_conf,
            dsn,
            )

        schema_xml = """
        <schema>
        <import package="ZODB"/>
        <section type="ZODB.database" name="main" attribute="database"/>
        </schema>
        """
        import ZConfig
        from StringIO import StringIO
        schema = ZConfig.loadSchemaFile(StringIO(schema_xml))
        config, handler = ZConfig.loadConfigFile(schema, StringIO(conf))

        db = config.database.open()
        try:
            storage = getattr(db, 'storage', None)
            if storage is None:
                # ZODB < 3.9
                storage = db._storage
            self.assertEqual(storage.isReadOnly(), False)
            self.assertEqual(storage.getName(), "xyz")
            adapter = storage._adapter
            from relstorage.adapters.postgresql import PostgreSQLAdapter
            self.assert_(isinstance(adapter, PostgreSQLAdapter))
            self.assertEqual(adapter._dsn, dsn)
            self.assertEqual(adapter.keep_history, self.keep_history)
            self.assertEqual(
                adapter.connmanager.replica_selector.replica_conf,
                replica_conf)
            self.assertEqual(storage._options.blob_chunk_size, 10485760)
        finally:
            db.close()
 def setUp(self):
     # Load ZConfig schema
     self.schema = ZConfig.loadSchema(farb.CONFIG_SCHEMA)
     rewrite_config(RELEASE_CONFIG_FILE_IN, RELEASE_CONFIG_FILE, CONFIG_SUBS)
     self.config, handler = ZConfig.loadConfig(self.schema, RELEASE_CONFIG_FILE)
     self.instSection = self.config.Installations.Installation[0]
     self.releaseSection = self.config.Releases.Release[0]
     self.instSectionNoCommands = self.config.Installations.Installation[1]
     self.instSectionNoDisks = self.config.Installations.Installation[2]
def loadConfiguration(file, url=None):
    global _schema
    if _schema is None:
        here = os.path.dirname(os.path.abspath(__file__))
        path = os.path.join(here, "schema", "productconfig.xml")
        _schema = ZConfig.loadSchema(path)
    data, handlers = ZConfig.loadConfigFile(_schema, file, url=url)
    return dict((sect.getSectionName(), sect.mapping)
                for sect in data.product_config)
Exemple #10
0
def options(args):
    """Password-specific options loaded from regular ZEO config file."""
    try:
        opts, args = getopt.getopt(args, "dr:p:f:C:", ["configure=",
                                                          "protocol=",
                                                          "filename=",
                                                          "realm"])
    except getopt.error as msg:
        usage(msg)
    config = None
    delete = 0
    auth_protocol = None
    auth_db = ""
    auth_realm = None
    for k, v in opts:
        if k == '-C' or k == '--configure':
            schemafile = os.path.join(os.path.dirname(ZEO.__file__),
                                                     "schema.xml")
            schema = ZConfig.loadSchema(schemafile)
            config, nil = ZConfig.loadConfig(schema, v)
        if k == '-d' or k == '--delete':
            delete = 1
        if k == '-p' or k == '--protocol':
            auth_protocol = v
        if k == '-f' or k == '--filename':
            auth_db = v
        if k == '-r' or k == '--realm':
            auth_realm = v

    if config is not None:
        if auth_protocol or auth_db:
            usage("Error: Conflicting options; use either -C *or* -p and -f")
        auth_protocol = config.zeo.authentication_protocol
        auth_db = config.zeo.authentication_database
        auth_realm = config.zeo.authentication_realm
    elif not (auth_protocol and auth_db):
        usage("Error: Must specifiy configuration file or protocol and database")

    password = None
    if delete:
        if not args:
            usage("Error: Must specify a username to delete")
        elif len(args) > 1:
            usage("Error: Too many arguments")
        username = args[0]
    else:
        if not args:
            usage("Error: Must specify a username")
        elif len(args) > 2:
            usage("Error: Too many arguments")
        elif len(args) == 1:
            username = args[0]
        else:
            username, password = args

    return auth_protocol, auth_db, auth_realm, delete, username, password
Exemple #11
0
def main(argv=sys.argv):
    parser = optparse.OptionParser(description=__doc__,
        usage="%prog [options] config_file")
    parser.add_option(
        "--dry-run", dest="dry_run", action="store_true",
        help="Attempt to open the storages, then explain what would be done")
    parser.add_option(
        "--clear", dest="clear", action="store_true",
        help="Clear the contents of the destination storage before copying")
    parser.set_defaults(dry_run=False, clear=False)
    options, args = parser.parse_args(argv[1:])

    if len(args) != 1:
        parser.error("The name of one configuration file is required.")

    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s [%(name)s] %(levelname)s %(message)s")

    schema = ZConfig.loadSchemaFile(StringIO(schema_xml))
    config, handler = ZConfig.loadConfig(schema, args[0])
    source = config.source.open()
    destination = config.destination.open()

    log.info("Storages opened successfully.")

    if options.dry_run:
        log.info("Dry run mode: not changing the destination.")
        if storage_has_data(destination):
            log.warning("The destination storage has data.")
        count = 0
        for txn in source.iterator():
            log.info('%s user=%s description=%s' % (
                TimeStamp(txn.tid), txn.user, txn.description))
            count += 1
        log.info("Would copy %d transactions.", count)

    else:
        if options.clear:
            log.info("Clearing old data...")
            if hasattr(destination, 'zap_all'):
                destination.zap_all()
            else:
                msg = ("Error: no API is known for clearing this type "
                       "of storage. Use another method.")
                sys.exit(msg)
            log.info("Done clearing old data.")

        if storage_has_data(destination):
            msg = "Error: the destination storage has data.  Try --clear."
            sys.exit(msg)

        destination.copyTransactionsFrom(source)
        source.close()
        destination.close()
def config(configfile, schemafile=None, features=()):
    # Load the configuration schema
    if schemafile is None:
        schemafile = os.path.join(
            os.path.dirname(appsetup.__file__), 'schema', 'schema.xml')

    # Let's support both, an opened file and path
    if isinstance(schemafile, basestring):
        schema = ZConfig.loadSchema(schemafile)
    else:
        schema = ZConfig.loadSchemaFile(schemafile)

    # Load the configuration file
    # Let's support both, an opened file and path
    try:
        if isinstance(configfile, basestring):
            options, handlers = ZConfig.loadConfig(schema, configfile)
        else:
            options, handlers = ZConfig.loadConfigFile(schema, configfile)
    except ZConfig.ConfigurationError as msg:
        sys.stderr.write("Error: %s\n" % str(msg))
        sys.exit(2)

    # Insert all specified Python paths
    if options.path:
        sys.path[:0] = [os.path.abspath(p) for p in options.path]

    # Parse product configs
    zope.app.appsetup.product.setProductConfigurations(
        options.product_config)

    # Setup the event log
    options.eventlog()

    # Setup other defined loggers
    for logger in options.loggers:
        logger()

    # Insert the devmode feature, if turned on
    if options.devmode:
        features += ('devmode',)
        logging.warning("Developer mode is enabled: this is a security risk "
            "and should NOT be enabled on production servers. Developer mode "
            "can usually be turned off by setting the `devmode` option to "
            "`off` or by removing it from the instance configuration file "
            "completely.")

    # Execute the ZCML configuration.
    appsetup.config(options.site_definition, features=features)

    # Connect to and open the database, notify subscribers.
    db = appsetup.multi_database(options.databases)[0][0]
    notify(zope.processlifetime.DatabaseOpened(db))

    return db
Exemple #13
0
    def readConfig(self, filename):
        # Read configuration file
        schema_string = open(self.ZCONFIG_SCHEMA).read()
        plugins = [configuration
                   for (configuration, handler) in plugin_configurations]
        schema_string = schema_string % {'plugins': "\n".join(plugins)}
        schema_file = StringIO(schema_string)

        schema = ZConfig.loadSchemaFile(schema_file, self.ZCONFIG_SCHEMA)

        config, handler = ZConfig.loadConfig(schema, filename)
        return config, handler
Exemple #14
0
    def _get_database_from_zconfig(self):
        settings = self.config.get_settings(self._zconfig_args)

        path = settings['path']
        frag = settings.get('frag', '')

        schema = ZConfig.loadSchemaFile(StringIO(self._schema_xml_template))
        config, _ = ZConfig.loadConfig(schema, path)
        for database in config.databases:
            if not frag or frag == database.name:
                return database.open()
        else:
            raise ValueError("Database %r not found." % frag)
Exemple #15
0
def main(argv=None):
    if argv is None:
        argv = sys.argv
    parser = argparse.ArgumentParser(description=__doc__)

    parser.add_argument(
        "-d", "--days", dest="days", default=0,
        help="Days of history to keep (default 0)",
        type=float,
    )
    parser.add_argument(
        "--prepack", dest="prepack", default=False,
        action="store_true",
        help="Perform only the pre-pack preparation stage of a pack. "
        "(Only works with some storage types)",
    )
    parser.add_argument(
        "--use-prepack-state", dest="reuse_prepack", default=False,
        action="store_true",
        help="Skip the preparation stage and go straight to packing. "
        "Requires that a pre-pack has been run, or that packing was aborted "
        "before it was completed.",
    )
    parser.add_argument("config_file")
    options = parser.parse_args(argv[1:])

    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s [%(name)s] %(levelname)s %(message)s")

    schema = ZConfig.loadSchemaFile(StringIO(schema_xml))
    config, _ = ZConfig.loadConfig(schema, options.config_file)

    t = time.time() - options.days * 86400.0
    for s in config.storages:
        name = '%s (%s)' % ((s.name or 'storage'), s.__class__.__name__)
        log.info("Opening %s...", name)
        storage = s.open()
        log.info("Packing %s.", name)
        if options.prepack or options.reuse_prepack:
            storage.pack(t, ZODB.serialize.referencesf,
                         prepack_only=options.prepack,
                         skip_prepack=options.reuse_prepack)
        else:
            # Be non-relstorage Storages friendly
            storage.pack(t, ZODB.serialize.referencesf)
        storage.close()
        log.info("Packed %s.", name)
Exemple #16
0
 def load(self, relurl, context=None):
     url = CONFIG_BASE + relurl
     self.conf, self.handlers = ZConfig.loadConfig(self.get_schema(), url)
     conf = self.conf
     self.assertTrue(conf.getSectionName() is None)
     self.assertTrue(conf.getSectionType() is None)
     return conf
Exemple #17
0
    def test_bad_section(self):
        self.schema = ZConfig.loadSchema(CONFIG_BASE + "simplesections.xml")
        self.assertRaisesRegex(ZConfig.ConfigurationSyntaxError,
                               'unexpected section end',
                               self.loadtext, '</close>')

        self.assertRaisesRegex(ZConfig.ConfigurationSyntaxError,
                               'unbalanced section end',
                               self.loadtext, '<section>\n</close>')

        self.assertRaisesRegex(ZConfig.ConfigurationSyntaxError,
                               'unclosed sections not allowed',
                               self.loadtext, '<section>\n')

        self.assertRaisesRegex(ZConfig.ConfigurationSyntaxError,
                               'malformed section header',
                               self.loadtext, '<section()>\n</close>')

        self.assertRaisesRegex(ZConfig.ConfigurationSyntaxError,
                               'malformed section end',
                               self.loadtext, '<section>\n</section')

        self.assertRaisesRegex(ZConfig.ConfigurationSyntaxError,
                               'malformed section start',
                               self.loadtext, '<section')

        # ConfigLoader.endSection raises this and it is recaught and
        # changed to a SyntaxError
        self.assertRaisesRegex(ZConfig.ConfigurationSyntaxError,
                               "no values for",
                               self.loadtext,
                               "<hasmin foo>\n</hasmin>")
Exemple #18
0
def get_storage(config_file):
    schema = ZConfig.loadSchemaFile(StringIO(schema_xml))
    config, dummy = ZConfig.loadConfig(schema, config_file)
    if len(config.storages) < 1:
        raise ValueError('No storages configured')
    connection = config.storages[0]
    if connection.config.keep_history:
        raise RuntimeError('Packing does not support history keeping storages')
    name = '%s (%s)' % ((connection.name or 'storage'),
                        connection.__class__.__name__)
    log.info("Opening %s...", name)
    storage = connection.open()
    log.info("Successfully openend %s", storage.getName())
    if 'PostgreSQLAdapter' not in storage.getName():
        raise RuntimeError('Only PostgreSQL databases are supported')
    return storage
Exemple #19
0
 def load_schema(self):
     if self.schema is None:
         # Load schema
         if self.schemadir is None:
             self.schemadir = os.path.dirname(__file__)
         self.schemafile = os.path.join(self.schemadir, self.schemafile)
         self.schema = ZConfig.loadSchema(self.schemafile)
Exemple #20
0
def databaseFromFile(f):
    """Create a database from a file object that provides configuration.

    See :func:`databaseFromString`.
    """
    config, handle = ZConfig.loadConfigFile(getDbSchema(), f)
    return databaseFromConfig(config.database)
Exemple #21
0
 def test_partition_softupdates(self):
     """ Verify that partition sizes are converted correctly """
     config, handler = ZConfig.loadConfig(self.schema, RELEASE_CONFIG_FILE)
     for part in config.Partitions.PartitionMap[0].Partition:
         if (part.type == 'swap'):
             # The swap partition should be 4GB, or 8,388,608 512-byte blocks
             self.assertEquals(part.size, 8388608)
Exemple #22
0
def databaseFromURL(url):
    """Load a database from URL (or file name) that provides configuration.

    See :func:`databaseFromString`.
    """
    config, handler = ZConfig.loadConfig(getDbSchema(), url)
    return databaseFromConfig(config.database)
Exemple #23
0
def main(argv=sys.argv):
    parser = optparse.OptionParser(description=__doc__,
        usage="%prog [options] config_file")
    parser.add_option(
        "-d", "--days", dest="days", default="0",
        help="Days of history to keep (default 0)",
    )
    parser.add_option(
        "--prepack", dest="prepack", default=False,
        action="store_true",
        help="Perform only the pre-pack preparation stage of a pack. "
        "(Only works with some storage types)",
    )
    parser.add_option(
        "--use-prepack-state", dest="reuse_prepack", default=False,
        action="store_true",
        help="Skip the preparation stage and go straight to packing. "
        "Requires that a pre-pack has been run, or that packing was aborted "
        "before it was completed.",
    )
    options, args = parser.parse_args(argv[1:])

    if len(args) != 1:
        parser.error("The name of one configuration file is required.")

    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s [%(name)s] %(levelname)s %(message)s")

    schema = ZConfig.loadSchemaFile(BytesIO(schema_xml))
    config, handler = ZConfig.loadConfig(schema, args[0])

    t = time.time() - float(options.days) * 86400.0
    for s in config.storages:
        name = '%s (%s)' % ((s.name or 'storage'), s.__class__.__name__)
        log.info("Opening %s...", name)
        storage = s.open()
        log.info("Packing %s.", name)
        if options.prepack or options.reuse_prepack:
            storage.pack(t, ZODB.serialize.referencesf,
                prepack_only=options.prepack,
                skip_prepack=options.reuse_prepack)
        else:
            # Be non-relstorage Storages friendly
            storage.pack(t, ZODB.serialize.referencesf)
        storage.close()
        log.info("Packed %s.", name)
Exemple #24
0
 def test_default_dists(self):
     """ Ensure that default Dists are set properly """
     subs = CONFIG_SUBS.copy()
     rewrite_config(RELEASE_CONFIG_FILE_IN, RELEASE_CONFIG_FILE, subs)
     config, handler = ZConfig.loadConfig(self.schema, RELEASE_CONFIG_FILE)
     release = config.Releases.Release[1]
     self.assertEquals(release.dists, ["base", "kernels", "doc", "games", "manpages", "catpages", "proflibs", "dict", "info", "src"])
     self.assertEquals(release.kerneldists, ["GENERIC", "SMP"])
Exemple #25
0
 def test_ports_cvs(self):
     """ Test using CVS for ports in binary release """
     subs = CONFIG_SUBS.copy()
     subs['@PORTSOURCE@'] = 'CVSRoot ' + CVSROOT
     rewrite_config(RELEASE_CONFIG_FILE_IN, RELEASE_CONFIG_FILE, subs)
     config, handler = ZConfig.loadConfig(self.schema, RELEASE_CONFIG_FILE)
     release = config.Releases.Release[2]
     self.assertEquals(release.cvsroot, CVSROOT)
Exemple #26
0
 def test_binary_release(self):
     """ Load a binary release configuration """
     config, handler = ZConfig.loadConfig(self.schema, RELEASE_CONFIG_FILE)
     release = config.Releases.Release[2]
     # Make sure options were set correctly
     self.assertEquals(release.binaryrelease, True)
     self.assertEquals(release.useportsnap, True)
     self.assertEquals(release.iso, os.path.join(DATA_DIR, 'fake_cd.iso'))
    def _setZConfig(self):
        """Modify the config, adding automatically generated settings"""
        schemafile = pkg_resources.resource_filename(
            'zope.app.server', 'schema.xml')
        schema = ZConfig.loadSchema(schemafile)
        root_options, handlers = ZConfig.loadConfig(
            schema, self.zope_config_file)

        # Devmode from the zope.app.server.main config, copied here for
        # ease of access.
        self.devmode = root_options.devmode

        # The defined servers.
        self.servers = root_options.servers

        # The number of configured threads.
        self.threads = root_options.threads
 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
     sio = cStringIO.StringIO(text.replace("<<INSTANCE_HOME>>", TEMPNAME))
     conf, handler = ZConfig.loadConfigFile(schema, sio)
     self.assertEqual(conf.instancehome, TEMPNAME)
     setConfiguration(conf)
Exemple #29
0
 def __call__(self, uri):
     (scheme, netloc, path, query, frag) = urlparse.urlsplit(uri)
      # urlparse doesnt understand file URLs and stuffs everything into path
     (scheme, netloc, path, query, frag) = urlparse.urlsplit('http:' + path)
     path = os.path.normpath(path)
     schema_xml = self.schema_xml_template
     schema = ZConfig.loadSchemaFile(StringIO(schema_xml))
     config, handler = ZConfig.loadConfig(schema, path)
     for database in config.databases:
         if not frag:
             # use the first defined in the file
             break
         elif frag == database.name:
             # match found
             break
     else:
         raise KeyError("No database named %s found" % frag)
     return (path, frag), (), {}, database.open
    def testGoodConfigLoad(self):
        """A bunch of correct configuration files"""

        global _good_configs
        for config in _good_configs:
            good_config = StringIO(config)
            conf, handler = ZConfig.loadConfigFile(self.schema, good_config)
            datatypes._paths = []
        return
Exemple #31
0
    def checkConfigureViaZConfig(self):
        import tempfile
        dsn = os.environ.get('ORACLE_TEST_DSN', 'XE')
        fd, replica_conf = tempfile.mkstemp()
        os.write(fd, dsn.encode("ascii"))
        os.close(fd)
        try:
            if self.keep_history:
                dbname = base_dbname
            else:
                dbname = base_dbname + '_hf'
            conf = u"""
            %%import relstorage
            <zodb main>
              <relstorage>
                name xyz
                read-only false
                keep-history %s
                replica-conf %s
                blob-chunk-size 10MB
                <oracle>
                  user %s
                  password relstoragetest
                  dsn %s
                </oracle>
              </relstorage>
            </zodb>
            """ % (
                self.keep_history and 'true' or 'false',
                replica_conf,
                dbname,
                dsn,
            )

            schema_xml = u"""
            <schema>
            <import package="ZODB"/>
            <section type="ZODB.database" name="main" attribute="database"/>
            </schema>
            """
            import ZConfig
            from io import StringIO
            schema = ZConfig.loadSchemaFile(StringIO(schema_xml))
            config, _handler = ZConfig.loadConfigFile(schema, StringIO(conf))

            db = config.database.open()
            try:
                storage = db.storage
                self.assertEqual(storage.isReadOnly(), False)
                self.assertEqual(storage.getName(), "xyz")
                adapter = storage._adapter
                from relstorage.adapters.oracle import OracleAdapter
                self.assertIsInstance(adapter, OracleAdapter)
                self.assertEqual(adapter._user, dbname)
                self.assertEqual(adapter._password, 'relstoragetest')
                self.assertEqual(adapter._dsn, dsn)
                self.assertEqual(adapter._twophase, False)
                self.assertEqual(adapter.keep_history, self.keep_history)
                self.assertEqual(
                    adapter.connmanager.replica_selector.replica_conf,
                    replica_conf)
                self.assertEqual(storage._options.blob_chunk_size, 10485760)
            finally:
                db.close()
        finally:
            os.remove(replica_conf)
Exemple #32
0
 def get_schema(self):
     if self.schema is None:
         ConfigurationTestCase.schema = ZConfig.loadSchema(CONFIG_BASE +
                                                           "simple.xml")
     return self.schema
Exemple #33
0
 def loadfile(self, file):
     schema = self.get_schema()
     self.conf, self.handlers = ZConfig.loadConfigFile(schema, file)
     return self.conf
import importlib_resources
from lazr.config import ConfigSchema
from lazr.config.interfaces import ConfigErrors
import scandir
import testtools
import ZConfig

import lp.services.config
from lp.services.config.fixture import ConfigUseFixture

# Configs that shouldn't be tested.
EXCLUDED_CONFIGS = ['lpnet-template']

# Calculate some landmark paths.
with importlib_resources.path('zope.app.server', 'schema.xml') as schema_file:
    schema = ZConfig.loadSchema(str(schema_file))

here = os.path.dirname(lp.services.config.__file__)
lazr_schema_file = os.path.join(here, 'schema-lazr.conf')


def make_test(config_file, description):
    def test_function():
        root, handlers = ZConfig.loadConfig(schema, config_file)
    # Hack the config file name into test_function's __name__ so that the test
    # -vv output is more informative. Unfortunately, FunctionTestCase's
    # description argument doesn't do what we want.
    test_function.__name__ = description
    return unittest.FunctionTestCase(test_function)

 def test_function():
     root, handlers = ZConfig.loadConfig(schema, config_file)
Exemple #36
0
 def get_config(self, text):
     conf, handler = ZConfig.loadConfigFile(self.get_schema(),
                                            StringIO(text))
     self.assertTrue(not handler)
     return conf
Exemple #37
0
 def check_load_from_path(self, path):
     schema = self.get_schema()
     ZConfig.loadConfig(schema, path)