'Run before each test method to initialize test environment.'

def setUp(self):

    super(TestCase, self).setUp()
    self.notifier = fake_notifier.get_fake_notifier()
    self.patch('cinder.rpc.get_notifier', side_effect=self._get_joined_notifier)
    if self.MOCK_WORKER:
        clean_path = 'cinder.objects.cleanable.CinderCleanableObject.%s'
        for method in ('create_worker', 'set_worker', 'unset_worker'):
            self.patch((clean_path % method), return_value=None)
    if self.MOCK_TOOZ:
        self.patch('cinder.coordination.Coordinator.start')
        self.patch('cinder.coordination.Coordinator.stop')
        self.patch('cinder.coordination.Coordinator.get_lock')
    i18n.enable_lazy(False)
    test_timeout = os.environ.get('OS_TEST_TIMEOUT', 0)
    try:
        test_timeout = int(test_timeout)
    except ValueError:
        test_timeout = 0
    if (test_timeout > 0):
        self.useFixture(fixtures.Timeout(test_timeout, gentle=True))
    self.useFixture(fixtures.NestedTempfile())
    self.useFixture(fixtures.TempHomeDir())
    environ_enabled = (lambda var_name: strutils.bool_from_string(os.environ.get(var_name)))
    if environ_enabled('OS_STDOUT_CAPTURE'):
        stdout = self.useFixture(fixtures.StringStream('stdout')).stream
        self.useFixture(fixtures.MonkeyPatch('sys.stdout', stdout))
    if environ_enabled('OS_STDERR_CAPTURE'):
        stderr = self.useFixture(fixtures.StringStream('stderr')).stream
        self.useFixture(fixtures.MonkeyPatch('sys.stderr', stderr))
    self.useFixture(log_fixture.get_logging_handle_error_fixture())
    self.useFixture(cinder_fixtures.StandardLogging())
    rpc.add_extra_exmods('cinder.tests.unit')
    self.addCleanup(rpc.clear_extra_exmods)
    self.addCleanup(rpc.cleanup)
    self.messaging_conf = messaging_conffixture.ConfFixture(CONF)
    self.messaging_conf.transport_driver = 'fake'
    self.messaging_conf.response_timeout = 15
    self.useFixture(self.messaging_conf)
    oslo_messaging.get_notification_transport(CONF)
    self.override_config('driver', ['test'], group='oslo_messaging_notifications')
    rpc.init(CONF)
    rpc.LAST_OBJ_VERSIONS = {}
    rpc.LAST_RPC_VERSIONS = {}
    conf_fixture.set_defaults(CONF)
    CONF([], default_config_files=[])
    self.start = timeutils.utcnow()
    CONF.set_default('connection', 'sqlite://', 'database')
    CONF.set_default('sqlite_synchronous', False, 'database')
    global _DB_CACHE
    if (not _DB_CACHE):
        _DB_CACHE = Database(sqla_api, migration, sql_connection=CONF.database.connection)
    self.useFixture(_DB_CACHE)
    objects_base.CinderObject.indirection_api = None
    self._base_test_obj_backup = copy.copy(objects_base.CinderObjectRegistry._registry._obj_classes)
    self.addCleanup(self._restore_obj_registry)
    mox_fixture = self.useFixture(moxstubout.MoxStubout())
    self.mox = mox_fixture.mox
    self.stubs = mox_fixture.stubs
    self.addCleanup(CONF.reset)
    self.addCleanup(self._common_cleanup)
    self.injected = []
    self._services = []
    fake_notifier.mock_notifier(self)
    self.override_config('fatal_exception_format_errors', True)
    lock_path = self.useFixture(fixtures.TempDir()).path
    self.fixture = self.useFixture(config_fixture.Config(lockutils.CONF))
    self.fixture.config(lock_path=lock_path, group='oslo_concurrency')
    lockutils.set_defaults(lock_path)
    self.override_config('policy_file', os.path.join(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')), self.POLICY_PATH), group='oslo_policy')
    self.override_config('resource_query_filters_file', os.path.join(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')), self.RESOURCE_FILTER_PATH))
    self._disable_osprofiler()
    self._disallow_invalid_uuids()
    sqla_api._GET_METHODS = {}
    self.override_config('backend_url', ('file://' + lock_path), group='coordination')
    coordination.COORDINATOR.start()
    self.addCleanup(coordination.COORDINATOR.stop)
