Esempio n. 1
0
    def setUp(self, initialize_hooks=True):
        super(TestAimDBBase, self).setUp()
        self.test_id = uuidutils.generate_uuid()
        aim_cfg.OPTION_SUBSCRIBER_MANAGER = None
        aci_universe.ws_context = None
        if not os.environ.get(K8S_STORE_VENV):
            CONF.set_override('aim_store', 'sql', 'aim')
            self.engine = api.get_engine()
            if not TestAimDBBase._TABLES_ESTABLISHED:
                model_base.Base.metadata.create_all(self.engine)
                TestAimDBBase._TABLES_ESTABLISHED = True

            # Uncomment the line below to log SQL statements. Additionally, to
            # log results of queries, change INFO to DEBUG
            #
            # logging.getLogger('sqlalchemy.engine').setLevel(logging.DEBUG)

            def clear_tables():
                with self.engine.begin() as conn:
                    for table in reversed(
                            model_base.Base.metadata.sorted_tables):
                        conn.execute(table.delete())

            self.addCleanup(clear_tables)
            self.old_add_commit_hook = (
                aim_store.SqlAlchemyStore.add_commit_hook)
            aim_store.SqlAlchemyStore.add_commit_hook = _add_commit_hook

            def restore_commit_hook():
                aim_store.SqlAlchemyStore.add_commit_hook = (
                    self.old_add_commit_hook)

            self.addCleanup(restore_commit_hook)
            aim_store.SqlAlchemyStore._after_transaction_end_2 = (
                _after_transaction_end_2)
        else:
            CONF.set_override('aim_store', 'k8s', 'aim')
            CONF.set_override('k8s_namespace', self.test_id, 'aim_k8s')
            k8s_config_path = os.environ.get(K8S_CONFIG_ENV)
            if k8s_config_path:
                CONF.set_override('k8s_config_path', k8s_config_path,
                                  'aim_k8s')
            aim_store.K8sStore._post_delete = _k8s_post_delete
            aim_store.K8sStore._post_create = _k8s_post_create
            global k8s_watcher_instance
            k8s_watcher_instance = k8s_watcher.K8sWatcher()
            k8s_watcher_instance.event_handler = mock.Mock()
            k8s_watcher_instance._renew_klient_watch = mock.Mock()
            self.addCleanup(self._cleanup_objects)

        self.store = api.get_store(expire_on_commit=True,
                                   initialize_hooks=initialize_hooks)
        self.ctx = context.AimContext(store=self.store)
        self.cfg_manager = aim_cfg.ConfigManager(self.ctx, '')
        self.tt_mgr = tree_manager.HashTreeManager()
        resource.ResourceBase.__eq__ = resource_equal
        self.cfg_manager.replace_all(CONF)
        self.sys_id = self.cfg_manager.get_option('aim_system_id', 'aim')
Esempio n. 2
0
def db_migration(ctx):
    alembic_path = os.path.abspath(
        os.path.join(os.path.dirname(migration.__file__),
                     'alembic.ini'))
    migrate_path = os.path.abspath(os.path.dirname(
        alembic_migrations.__file__))
    migration_config = {'alembic_ini_path': alembic_path,
                        'alembic_repo_path': migrate_path}
    ctx.obj['manager'] = manager.MigrationManager(migration_config,
                                                  engine=api.get_engine())
    config.setup_logging()
    def setUp(self, mock_store=True):
        super(TestAimDBBase, self).setUp()
        self.test_id = uuidutils.generate_uuid()
        aim_cfg.OPTION_SUBSCRIBER_MANAGER = None
        aci_universe.ws_context = None
        if not os.environ.get(K8S_STORE_VENV):
            CONF.set_override('aim_store', 'sql', 'aim')
            self.engine = api.get_engine()
            if not TestAimDBBase._TABLES_ESTABLISHED:
                model_base.Base.metadata.create_all(self.engine)
                TestAimDBBase._TABLES_ESTABLISHED = True

            # Uncomment the line below to log SQL statements. Additionally, to
            # log results of queries, change INFO to DEBUG
            #
            # logging.getLogger('sqlalchemy.engine').setLevel(logging.DEBUG)

            def clear_tables():
                with self.engine.begin() as conn:
                    for table in reversed(
                            model_base.Base.metadata.sorted_tables):
                        conn.execute(table.delete())
            self.addCleanup(clear_tables)
            if mock_store:
                self.old_initialize_hooks = (
                    aim_store.SqlAlchemyStore._initialize_hooks)
                aim_store.SqlAlchemyStore.old_initialize_hooks = (
                    self.old_initialize_hooks)
                aim_store.SqlAlchemyStore._initialize_hooks = _initialize_hooks

                def restore_initialize_hook():
                    aim_store.SqlAlchemyStore._initialize_hooks = (
                        self.old_initialize_hooks)
                self.addCleanup(restore_initialize_hook)
                aim_store.SqlAlchemyStore._catch_up_logs = _catch_up_logs
        else:
            CONF.set_override('aim_store', 'k8s', 'aim')
            CONF.set_override('k8s_namespace', self.test_id, 'aim_k8s')
            k8s_config_path = os.environ.get(K8S_CONFIG_ENV)
            if k8s_config_path:
                CONF.set_override('k8s_config_path', k8s_config_path,
                                  'aim_k8s')
            aim_store.K8sStore._post_delete = _k8s_post_delete
            aim_store.K8sStore._post_create = _k8s_post_create
            global k8s_watcher_instance
            k8s_watcher_instance = k8s_watcher.K8sWatcher()
            k8s_watcher_instance.event_handler = mock.Mock()
            k8s_watcher_instance._renew_klient_watch = mock.Mock()
            self.addCleanup(self._cleanup_objects)

        self.store = api.get_store(expire_on_commit=True)

        def unregister_catch_up():
            self.store.unregister_after_transaction_ends_callback(
                '_catch_up_logs')

        self.addCleanup(unregister_catch_up)
        self.ctx = context.AimContext(store=self.store)
        self.cfg_manager = aim_cfg.ConfigManager(self.ctx, '')
        self.tt_mgr = tree_manager.HashTreeManager()
        resource.ResourceBase.__eq__ = resource_equal
        self.cfg_manager.replace_all(CONF)
        self.sys_id = self.cfg_manager.get_option('aim_system_id', 'aim')