Do not initialize anything during import phase

The structure of the program is compromised by doing too much logic in
the import phase. We can read the code more cleanly if we can see where
the intended initialization code is called. This may make tests slightly
more tedious to write, but that should be handled by writing generic
fixtures which can be reused for exactly this purpose.

Change-Id: I1d221e2f90d1f6e89717a37d2128caabd077d30a
This commit is contained in:
Clint Byrum 2013-04-23 15:51:57 -07:00
parent f3be3d90b3
commit a60f6d4569
16 changed files with 34 additions and 52 deletions

View File

@ -1,22 +0,0 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# 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 abstraction for Heat.'''
from heat.common import config
config.register_db_opts()
from heat.db.api import *

View File

@ -28,6 +28,7 @@ supported backend.
from oslo.config import cfg
from heat.common import config
from heat.db import utils
SQL_CONNECTION = 'sqlite://'
@ -44,6 +45,7 @@ IMPL = utils.LazyPluggable('db_backend',
def configure():
global SQL_CONNECTION
global SQL_IDLE_TIMEOUT
config.register_db_opts()
SQL_CONNECTION = cfg.CONF.sql_connection
SQL_IDLE_TIMEOUT = cfg.CONF.sql_idle_timeout

View File

@ -1,19 +0,0 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# 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.
from heat.common import config
config.register_engine_opts()
from heat import db # pyflakes_bypass register DB options

View File

@ -21,6 +21,7 @@ from heat.common import exception
from heat.engine import dependencies
from heat.common import identifier
from heat.engine import resource
from heat.engine import resources
from heat.engine import template
from heat.engine import timestamp
from heat.engine.parameters import Parameters
@ -84,6 +85,8 @@ class Stack(object):
self.timeout_mins = timeout_mins
self.disable_rollback = disable_rollback
resources.initialise()
if parameters is None:
parameters = Parameters(self.name, self.t)
self.parameters = parameters

View File

@ -42,10 +42,19 @@ def _register_modules(modules):
_register_resources(itertools.chain.from_iterable(resource_lists))
def _initialise():
_initialized = False
def initialise():
global _initialized
if _initialized:
return
import sys
from heat.common import config
from heat.common import plugin_loader
config.register_engine_opts()
_register_modules(plugin_loader.load_modules(sys.modules[__name__]))
from oslo.config import cfg
@ -53,6 +62,4 @@ def _initialise():
plugin_pkg = plugin_loader.create_subpackage(cfg.CONF.plugin_dirs,
'heat.engine')
_register_modules(plugin_loader.load_modules(plugin_pkg, True))
_initialise()
_initialized = True

View File

@ -19,6 +19,7 @@ import json
from oslo.config import cfg
import webob
from heat.common import config
from heat.common import context
from heat.db import api as db_api
from heat.engine import api
@ -65,6 +66,7 @@ class EngineService(service.Service):
super(EngineService, self).__init__(host, topic)
# stg == "Stack Thread Groups"
self.stg = {}
config.register_engine_opts()
def _start_in_thread(self, stack_id, func, *args, **kwargs):
if stack_id not in self.stg:

View File

@ -25,7 +25,7 @@ from heat.common import context
from heat.common import exception
from heat.tests.v1_1 import fakes
import heat.engine.api as engine_api
import heat.db as db_api
import heat.db.api as db_api
from heat.common import identifier
from heat.common import template_format
from heat.engine import parser

View File

@ -18,7 +18,7 @@ import mox
import unittest
from heat.common import context
import heat.db as db_api
import heat.db.api as db_api
from heat.engine import parser
from heat.engine import resource
from heat.engine import template

View File

@ -23,6 +23,7 @@ from nose.plugins.attrib import attr
from oslo.config import cfg
from heat.common import exception
from heat.common import config
from heat.common import context
from heat.common import template_format
from heat.engine import parser
@ -49,6 +50,7 @@ def create_context(mocks, user='lb_test_user',
@attr(speed='fast')
class LoadBalancerTest(unittest.TestCase):
def setUp(self):
config.register_engine_opts()
self.m = mox.Mox()
self.fc = fakes.FakeClient()
self.m.StubOutWithMock(lb.LoadBalancer, 'nova')

View File

@ -29,7 +29,7 @@ from heat.engine import template
from heat.tests.utils import stack_delete_after
from heat.tests import generic_resource as generic_rsrc
import heat.db as db_api
import heat.db.api as db_api
def join(raw):

View File

@ -24,6 +24,7 @@ from oslo.config import cfg
import stubout
import unittest
from heat.common import config
from heat.common import context
from heat.common import identifier
from heat.rpc import client as rpc_client
@ -34,6 +35,7 @@ from heat.openstack.common import rpc
class EngineRpcAPITestCase(unittest.TestCase):
def setUp(self):
config.register_engine_opts()
self.context = context.get_admin_context()
cfg.CONF.set_default('rpc_backend',
'heat.openstack.common.rpc.impl_fake')

View File

@ -28,7 +28,6 @@ from heat.tests.v1_1 import fakes
from novaclient.v1_1 import security_groups as nova_sg
from novaclient.v1_1 import security_group_rules as nova_sgr
NovaSG = collections.namedtuple('NovaSG',
' '.join([
'name',

View File

@ -20,6 +20,7 @@ import mox
from nose.plugins.attrib import attr
from oslo.config import cfg
from heat.common import config
from heat.common import context
from heat.common import exception
from heat.common import template_format
@ -34,6 +35,7 @@ import keystoneclient.exceptions
@attr(speed='fast')
class UserTest(unittest.TestCase):
def setUp(self):
config.register_engine_opts()
self.m = mox.Mox()
self.fc = fakes.FakeKeystoneClient(username='test_stack.CfnUser')
cfg.CONF.set_default('heat_stack_user_role', 'stack_user_role')
@ -225,6 +227,7 @@ class UserTest(unittest.TestCase):
@attr(speed='fast')
class AccessKeyTest(unittest.TestCase):
def setUp(self):
config.register_engine_opts()
self.m = mox.Mox()
self.fc = fakes.FakeKeystoneClient(username='test_stack.CfnUser')
cfg.CONF.set_default('heat_stack_user_role', 'stack_user_role')

View File

@ -23,7 +23,7 @@ from heat.common import exception
from heat.common import template_format
from heat.engine.resources import instance as instances
from heat.engine import service
import heat.db as db_api
import heat.db.api as db_api
from heat.engine import parser
test_template_volumeattach = '''

View File

@ -26,11 +26,12 @@ import unittest
from heat.tests import fakes
from heat.tests.utils import stack_delete_after
import heat.db as db_api
import heat.db.api as db_api
from heat.common import template_format
from heat.common import identifier
from heat.engine import parser
from heat.engine.resources import wait_condition as wc
from heat.common import config
from heat.common import context
test_template_waitcondition = '''
@ -79,6 +80,7 @@ test_template_wc_count = '''
@attr(speed='slow')
class WaitConditionTest(unittest.TestCase):
def setUp(self):
config.register_engine_opts()
self.m = mox.Mox()
self.m.StubOutWithMock(wc.WaitConditionHandle,
'get_status')
@ -381,6 +383,7 @@ class WaitConditionTest(unittest.TestCase):
@attr(speed='fast')
class WaitConditionHandleTest(unittest.TestCase):
def setUp(self):
config.register_engine_opts()
self.m = mox.Mox()
cfg.CONF.set_default('heat_waitcondition_server_url',
'http://127.0.0.1:8000/v1/waitcondition')

View File

@ -18,7 +18,7 @@ import mox
from nose.plugins.attrib import attr
import unittest
from heat.common import context
import heat.db as db_api
import heat.db.api as db_api
from heat.openstack.common import timeutils
from heat.engine import watchrule