placement/placement/db_api.py
Chris Dent c04a107e32 Make the PlacementFixture usable without intercept
There are cases where it is necessary to use the PlacementFixture
without it running a wsgi-intercept, for example when you'd like
to run an indepdendent wsgi-intercept, as in the nova tests that
exercise the report client.

In the bug listed as partial below those report client tests were
using a nova conf global to initialize the wsgi intercept. They
just happened to be working because that CONF has the required
configuration settings. When placement's conf diverged [1] things
broke. A nova change which depends on this one will resolve the
bug.

Note that as a part of this change, the code which deletes
'sync_on_startup' (the new conf option) before configuring the
database, is adapted to be more flexible. There really is only
one conf setting that is required to start the placement database,
connection, and we'd like to make it possible for people to set
just that when doing tests or experiments and not have to worry
about extraneous optional things.

Some parameter descriptions are added to the PlacementFixture
__init__.

We should add more robust testing to all three of PlacementFixture,
fixtures.Database, and PlacementDirect, but at the moment we are
trying to fix a gate blocking bug.

Partial-Bug: #1818560

[1] Ie43a69be8b75250d9deca6a911eda7b722ef8648

Change-Id: I8c36f35dbe85b0c0db1a5b6b5389b160b68ca488
2019-03-04 20:55:22 +00:00

46 lines
1.6 KiB
Python

# 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.
"""Database context manager for placement database connection."""
from oslo_db.sqlalchemy import enginefacade
from oslo_log import log as logging
from placement.util import run_once
LOG = logging.getLogger(__name__)
placement_context_manager = enginefacade.transaction_context()
def _get_db_conf(conf_group):
conf_dict = dict(conf_group.items())
# Remove the 'sync_on_startup' conf setting, enginefacade does not use it.
# Use pop since it might not be present in testing situations and we
# don't want to care here.
conf_dict.pop('sync_on_startup', None)
return conf_dict
@run_once("TransactionFactory already started, not reconfiguring.",
LOG.warning)
def configure(conf):
placement_context_manager.configure(
**_get_db_conf(conf.placement_database))
def get_placement_engine():
return placement_context_manager.writer.get_engine()
@enginefacade.transaction_context_provider
class DbContext(object):
"""Stub class for db session handling outside of web requests."""