Beispiel #1
0
    def test_api_fixture_cleanup(self):
        # This sets up reasonable db connection strings
        self.useFixture(fixtures.ConfFixture())
        fix = fixtures.Database(database='api')
        self.useFixture(fix)

        # No data inserted by migrations so we need to add a row
        engine = api_db_api.get_engine()
        conn = engine.connect()
        uuid = uuidutils.generate_uuid()
        conn.execute(
            sa.text(
                "INSERT INTO cell_mappings (uuid, name) VALUES (:uuid, :name)"
            ),
            uuid=uuid,
            name='fake-cell',
        )
        result = conn.execute(sa.text("SELECT * FROM cell_mappings"))
        rows = result.fetchall()
        self.assertEqual(1, len(rows), "Rows %s" % rows)

        # Manually do the cleanup that addCleanup will do
        fix.cleanup()

        # Ensure the db contains nothing
        engine = api_db_api.get_engine()
        conn = engine.connect()
        schema = "".join(line for line in conn.connection.iterdump())
        self.assertEqual("BEGIN TRANSACTION;COMMIT;", schema)
Beispiel #2
0
    def test_api_fixture_reset(self):
        # This sets up reasonable db connection strings
        self.useFixture(fixtures.ConfFixture())
        db_fixture = fixtures.Database(database='api')
        self.useFixture(db_fixture)
        engine = api_db_api.get_engine()
        conn = engine.connect()
        result = conn.execute(sa.text("SELECT * FROM cell_mappings"))
        rows = result.fetchall()
        self.assertEqual(0, len(rows), "Rows %s" % rows)

        uuid = uuidutils.generate_uuid()
        conn.execute(
            sa.text(
                "INSERT INTO cell_mappings (uuid, name) VALUES (:uuid, :name)"
            ),
            uuid=uuid,
            name='fake-cell',
        )
        result = conn.execute(sa.text("SELECT * FROM cell_mappings"))
        rows = result.fetchall()
        self.assertEqual(1, len(rows), "Rows %s" % rows)

        # NOTE(sdague): it's important to reestablish the db
        # connection because otherwise we have a reference to the old
        # in mem db.
        db_fixture.reset()
        engine = api_db_api.get_engine()
        conn = engine.connect()
        result = conn.execute(sa.text("SELECT * FROM cell_mappings"))
        rows = result.fetchall()
        self.assertEqual(0, len(rows), "Rows %s" % rows)
Beispiel #3
0
    def test_api_fixture_reset(self):
        # This sets up reasonable db connection strings
        self.useFixture(fixtures.ConfFixture())
        self.useFixture(fixtures.Database(database='api'))
        engine = api_db_api.get_engine()
        conn = engine.connect()
        result = conn.execute("select * from cell_mappings")
        rows = result.fetchall()
        self.assertEqual(0, len(rows), "Rows %s" % rows)

        uuid = uuidutils.generate_uuid()
        conn.execute("insert into cell_mappings (uuid, name) VALUES "
                     "('%s', 'fake-cell')" % (uuid, ))
        result = conn.execute("select * from cell_mappings")
        rows = result.fetchall()
        self.assertEqual(1, len(rows), "Rows %s" % rows)

        # reset by invoking the fixture again
        #
        # NOTE(sdague): it's important to reestablish the db
        # connection because otherwise we have a reference to the old
        # in mem db.
        self.useFixture(fixtures.Database(database='api'))
        conn = engine.connect()
        result = conn.execute("select * from cell_mappings")
        rows = result.fetchall()
        self.assertEqual(0, len(rows), "Rows %s" % rows)
Beispiel #4
0
    def _check_cellsv2(self):
        """Checks to see if cells v2 has been setup.

        These are the same checks performed in the 030_require_cell_setup API
        DB migration except we expect this to be run AFTER the
        nova-manage cell_v2 simple_cell_setup command, which would create the
        cell and host mappings and sync the cell0 database schema, so we don't
        check for flavors at all because you could create those after doing
        this on an initial install. This also has to be careful about checking
        for compute nodes if there are no host mappings on a fresh install.
        """
        meta = sa.MetaData()
        engine = api_db_api.get_engine()

        cell_mappings = self._get_cell_mappings()
        count = len(cell_mappings)
        # Two mappings are required at a minimum, cell0 and your first cell
        if count < 2:
            msg = _('There needs to be at least two cell mappings, one for '
                    'cell0 and one for your first cell. Run command '
                    '\'nova-manage cell_v2 simple_cell_setup\' and then '
                    'retry.')
            return upgradecheck.Result(upgradecheck.Code.FAILURE, msg)

        cell0 = any(mapping.is_cell0() for mapping in cell_mappings)
        if not cell0:
            msg = _('No cell0 mapping found. Run command '
                    '\'nova-manage cell_v2 simple_cell_setup\' and then '
                    'retry.')
            return upgradecheck.Result(upgradecheck.Code.FAILURE, msg)

        host_mappings = sa.Table('host_mappings', meta, autoload_with=engine)

        with engine.connect() as conn:
            count = conn.execute(
                sa.select(sqlfunc.count()).select_from(host_mappings)
            ).scalars().first()

        if count == 0:
            # This may be a fresh install in which case there may not be any
            # compute_nodes in the cell database if the nova-compute service
            # hasn't started yet to create those records. So let's query the
            # cell database for compute_nodes records and if we find at least
            # one it's a failure.
            num_computes = self._count_compute_nodes()
            if num_computes > 0:
                msg = _('No host mappings found but there are compute nodes. '
                        'Run command \'nova-manage cell_v2 '
                        'simple_cell_setup\' and then retry.')
                return upgradecheck.Result(upgradecheck.Code.FAILURE, msg)

            msg = _('No host mappings or compute nodes were found. Remember '
                    'to run command \'nova-manage cell_v2 discover_hosts\' '
                    'when new compute hosts are deployed.')
            return upgradecheck.Result(upgradecheck.Code.SUCCESS, msg)

        return upgradecheck.Result(upgradecheck.Code.SUCCESS)
Beispiel #5
0
    def test_flavors(self, mock_send_notification):
        self.useFixture(fixtures.ConfFixture())
        self.useFixture(fixtures.Database())
        self.useFixture(fixtures.Database(database='api'))

        engine = api_db_api.get_engine()
        conn = engine.connect()
        result = conn.execute("select * from flavors")
        rows = result.fetchall()
        self.assertEqual(0, len(rows), "Rows %s" % rows)

        self.useFixture(fixtures.DefaultFlavorsFixture())

        result = conn.execute("select * from flavors")
        rows = result.fetchall()
        self.assertEqual(6, len(rows), "Rows %s" % rows)
Beispiel #6
0
 def test_get_engine(self, mock_ctxt_mgr):
     db_api.get_engine()
     mock_ctxt_mgr.writer.get_engine.assert_called_once_with()
Beispiel #7
0
def _get_engine(database='main', context=None):
    if database == 'main':
        return main_db_api.get_engine(context=context)

    if database == 'api':
        return api_db_api.get_engine()