Add per-test database fixture

And test it.

Change-Id: I49fb5f58127ed2a1c80282b55e30336da725b75c
This commit is contained in:
James E. Blair 2014-03-25 09:49:52 -07:00
parent faef2431a7
commit 852c6b0b96
3 changed files with 76 additions and 1 deletions

View File

@ -38,7 +38,7 @@ If the cloud being used has no default_floating_pool defined in nova.conf,
you will need to define a pool name using the nodepool yaml file to use
floating ips.
Set up database:
Set up database for interactive testing:
.. code-block:: bash
@ -48,6 +48,15 @@ Set up database:
mysql> GRANT ALL ON nodepool.* TO 'nodepool'@'localhost';
mysql> flush privileges;
Set up database for unit tests:
.. code-block:: bash
mysql -u root
mysql> grant all privileges on *.* to 'openstack_citest' identified by 'openstack_citest' with grant option;
mysql> flush privileges;
mysql> create database openstack_citest;
Export variable for your ssh key so you can log into the created instances:
.. code-block:: bash

View File

@ -15,7 +15,10 @@
"""Common utilities used in testing"""
import MySQLdb
import os
import random
import string
import fixtures
import testresources
@ -60,3 +63,42 @@ class AllocatorTestCase(BaseTestCase):
print self.agt[i]
for i, amount in enumerate(self.results):
self.assertEqual(self.agt[i].amount, amount)
class MySQLSchemaFixture(fixtures.Fixture):
def setUp(self):
super(MySQLSchemaFixture, self).setUp()
random_bits = ''.join(random.choice(string.ascii_lowercase +
string.ascii_uppercase)
for x in range(8))
self.name = '%s_%s' % (random_bits, os.getpid())
db = MySQLdb.connect(host="localhost",
user="openstack_citest",
passwd="openstack_citest",
db="openstack_citest")
cur = db.cursor()
cur.execute("create database %s" % self.name)
cur.execute("grant all on %s.* to '%s'@'localhost'" %
(self.name, self.name))
cur.execute("flush privileges")
self.dburi = 'mysql://%s@localhost/%s' % (self.name, self.name)
self.addDetail('dburi', testtools.content.text_content(self.dburi))
self.addCleanup(self.cleanup)
def cleanup(self):
db = MySQLdb.connect(host="localhost",
user="openstack_citest",
passwd="openstack_citest",
db="openstack_citest")
cur = db.cursor()
cur.execute("drop database %s" % self.name)
class DBTestCase(BaseTestCase):
def setUp(self):
super(DBTestCase, self).setUp()
f = MySQLSchemaFixture()
self.useFixture(f)
self.dburi = f.dburi

View File

@ -0,0 +1,24 @@
# Copyright (C) 2014 OpenStack Foundation
#
# 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 nodepool import tests
from nodepool import nodedb
class TestNodepool(tests.DBTestCase):
def test_db(self):
db = nodedb.NodeDatabase(self.dburi)
with db.getSession() as session:
session.getNodes()