Allow setting DB for unittests via env & add psql support

RALLY_UNITTEST_DB_URL may be used to change test database.

Sample usage:

export RALLY_UNITTEST_DB_URL="mysql://user:secret@localhost/rally"
tox -epy27

Fix postgress related bugs in unit tests.

* All enums should have name
* Fix invalid unit tests

Closes-bug: #1413155
Partial-bug: #1405884

Co-Authored-By: Sergey Skripnick <sskripnick@mirantis.com>
Co-Authored-By: Boris Pavlovic <boris@pavlovic.me>

Change-Id: Ic741cb79eadf952c3dea99182c1e7d52992c9904
This commit is contained in:
Sergey Skripnick 2015-01-21 14:14:51 +02:00 committed by Boris Pavlovic
parent 6de4598de1
commit 2455db7f64
8 changed files with 251 additions and 217 deletions

View File

@ -71,14 +71,14 @@ init_variables() {
install_rhel_based_system_requirements() {
local install_rally_dependencies='wget'
local cryptography_dependencies='gcc libffi-devel python-devel openssl-devel gmp-devel'
local cryptography_dependencies='gcc libffi-devel python-devel openssl-devel gmp-devel libpq-devel'
local external_dependencies='libxml2-devel libxslt-devel' # dependencies from projects, which are used by rally
yum -y install ${install_rally_dependencies} ${cryptography_dependencies} ${external_dependencies}
}
install_debian_based_system_requirements() {
local install_rally_dependencies='wget'
local cryptography_dependencies='build-essential libssl-dev libffi-dev python-dev'
local cryptography_dependencies='build-essential libssl-dev libffi-dev python-dev libpq-dev'
local external_dependencies='libxml2-dev libxslt1-dev' # dependencies from projects, which are used by rally
apt-get -y install ${install_rally_dependencies} ${cryptography_dependencies} ${external_dependencies}
}

View File

@ -50,7 +50,7 @@ CONF = cfg.CONF
db_options.set_defaults(CONF, connection="sqlite:////tmp/rally.sqlite",
sqlite_db="rally.sqlite")
_BACKEND_MAPPING = {'sqlalchemy': 'rally.db.sqlalchemy.api'}
_BACKEND_MAPPING = {"sqlalchemy": "rally.db.sqlalchemy.api"}
IMPL = db_api.DBAPI.from_config(CONF, backend_mapping=_BACKEND_MAPPING)
@ -327,7 +327,7 @@ def register_worker(values):
:param values: A dict of values which must contain the following:
{
'hostname': the unique hostname which identifies
"hostname": the unique hostname which identifies
this worker service.
}
:returns: A worker.
@ -356,7 +356,7 @@ def unregister_worker(hostname):
def update_worker(hostname):
"""Mark a worker as active by updating its 'updated_at' property.
"""Mark a worker as active by updating its "updated_at" property.
:param hostname: The hostname of this worker service.
:raises: WorkerNotFound

View File

@ -105,12 +105,12 @@ class Connection(object):
def task_get_detailed(self, uuid):
return (self.model_query(models.Task).
options(sa.orm.joinedload('results')).
options(sa.orm.joinedload("results")).
filter_by(uuid=uuid).first())
def task_get_detailed_last(self):
return (self.model_query(models.Task).
options(sa.orm.joinedload('results')).
options(sa.orm.joinedload("results")).
order_by(models.Task.id.desc()).first())
def task_create(self, values):
@ -121,7 +121,7 @@ class Connection(object):
def task_update(self, uuid, values):
session = get_session()
values.pop('uuid', None)
values.pop("uuid", None)
with session.begin():
task = self._task_get(uuid, session=session)
task.update(values)
@ -132,9 +132,9 @@ class Connection(object):
filters = {}
if status is not None:
filters['status'] = status
filters["status"] = status
if deployment is not None:
filters['deployment_uuid'] = self.deployment_get(
filters["deployment_uuid"] = self.deployment_get(
deployment)["uuid"]
if filters:
@ -212,7 +212,7 @@ class Connection(object):
def deployment_update(self, deployment, values):
session = get_session()
values.pop('uuid', None)
values.pop("uuid", None)
with session.begin():
dpl = self._deployment_get(deployment, session=session)
dpl.update(values)
@ -306,12 +306,12 @@ class Connection(object):
try:
worker = models.Worker()
worker.update(values)
worker.update({'updated_at': timeutils.utcnow()})
worker.update({"updated_at": timeutils.utcnow()})
worker.save()
return worker
except db_exc.DBDuplicateEntry:
raise exceptions.WorkerAlreadyRegistered(
worker=values['hostname'])
worker=values["hostname"])
def get_worker(self, hostname):
try:
@ -329,6 +329,6 @@ class Connection(object):
def update_worker(self, hostname):
count = (self.model_query(models.Worker).
filter_by(hostname=hostname).
update({'updated_at': timeutils.utcnow()}))
update({"updated_at": timeutils.utcnow()}))
if count == 0:
raise exceptions.WorkerNotFound(worker=hostname)

View File

@ -18,7 +18,8 @@ SQLAlchemy models for rally data.
import uuid
from oslo.db.sqlalchemy import models
from oslo_db.sqlalchemy.compat import utils as compat_utils
from oslo_db.sqlalchemy import models
import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import schema
@ -52,15 +53,15 @@ class Deployment(BASE, RallyBase):
"""Represent a deployment of OpenStack."""
__tablename__ = "deployments"
__table_args__ = (
sa.Index('deployment_uuid', 'uuid', unique=True),
sa.Index('deployment_parent_uuid', 'parent_uuid'),
sa.Index("deployment_uuid", "uuid", unique=True),
sa.Index("deployment_parent_uuid", "parent_uuid"),
)
id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
uuid = sa.Column(sa.String(36), default=UUID, nullable=False)
parent_uuid = sa.Column(
sa.String(36),
sa.ForeignKey(uuid, use_alter=True, name='fk_parent_uuid'),
sa.ForeignKey(uuid, use_alter=True, name="fk_parent_uuid"),
default=None,
)
name = sa.Column(sa.String(255), unique=True)
@ -83,8 +84,8 @@ class Deployment(BASE, RallyBase):
users = sa.Column(types.PickleType, default=[], nullable=False)
status = sa.Column(
sa.Enum(*consts.DeployStatus),
name='enum_deployments_status',
sa.Enum(*consts.DeployStatus, name="enum_deploy_status"),
name="enum_deployments_status",
default=consts.DeployStatus.DEPLOY_INIT,
nullable=False,
)
@ -99,13 +100,13 @@ class Deployment(BASE, RallyBase):
class Resource(BASE, RallyBase):
"""Represent a resource of a deployment."""
__tablename__ = 'resources'
__tablename__ = "resources"
__table_args__ = (
sa.Index('resource_deployment_uuid', 'deployment_uuid'),
sa.Index('resource_provider_name', 'deployment_uuid', 'provider_name'),
sa.Index('resource_type', 'deployment_uuid', 'type'),
sa.Index('resource_provider_name_and_type', 'deployment_uuid',
'provider_name', 'type'),
sa.Index("resource_deployment_uuid", "deployment_uuid"),
sa.Index("resource_provider_name", "deployment_uuid", "provider_name"),
sa.Index("resource_type", "deployment_uuid", "type"),
sa.Index("resource_provider_name_and_type", "deployment_uuid",
"provider_name", "type"),
)
id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
@ -125,7 +126,7 @@ class Resource(BASE, RallyBase):
)
deployment = sa.orm.relationship(
Deployment,
backref=sa.orm.backref('resources'),
backref=sa.orm.backref("resources"),
foreign_keys=deployment_uuid,
primaryjoin=(deployment_uuid == Deployment.uuid),
)
@ -146,8 +147,8 @@ class Task(BASE, RallyBase):
name="enum_tasks_status"),
default=consts.TaskStatus.INIT,
nullable=False)
verification_log = sa.Column(sa.Text, default='')
tag = sa.Column(sa.String(64), default='')
verification_log = sa.Column(sa.Text, default="")
tag = sa.Column(sa.String(64), default="")
deployment_uuid = sa.Column(
sa.String(36),
@ -172,11 +173,11 @@ class TaskResult(BASE, RallyBase):
key = sa.Column(sa_types.MutableJSONEncodedDict, nullable=False)
data = sa.Column(sa_types.BigMutableJSONEncodedDict, nullable=False)
task_uuid = sa.Column(sa.String(36), sa.ForeignKey('tasks.uuid'))
task_uuid = sa.Column(sa.String(36), sa.ForeignKey("tasks.uuid"))
task = sa.orm.relationship(Task,
backref=sa.orm.backref('results'),
backref=sa.orm.backref("results"),
foreign_keys=task_uuid,
primaryjoin='TaskResult.task_uuid == Task.uuid')
primaryjoin="TaskResult.task_uuid == Task.uuid")
class Verification(BASE, RallyBase):
@ -215,7 +216,7 @@ class VerificationResult(BASE, RallyBase):
id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
verification_uuid = sa.Column(sa.String(36),
sa.ForeignKey('verifications.uuid'))
sa.ForeignKey("verifications.uuid"))
data = sa.Column(sa_types.BigMutableJSONEncodedDict, nullable=False)
@ -223,7 +224,7 @@ class VerificationResult(BASE, RallyBase):
class Worker(BASE, RallyBase):
__tablename__ = "workers"
__table_args__ = (
schema.UniqueConstraint('hostname', name='uniq_worker@hostname'),
schema.UniqueConstraint("hostname", name="uniq_worker@hostname"),
)
id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)
hostname = sa.Column(sa.String(255))
@ -235,10 +236,46 @@ def create_db():
BASE.metadata.create_all(sa_api.get_engine())
# TODO(boris-42): Remove it after oslo.db > 1.4.1 will be released.
def drop_all_objects(engine):
"""Drop all database objects.
Drops all database objects remaining on the default schema of the given
engine. Per-db implementations will also need to drop items specific to
those systems, such as sequences, custom types (e.g. pg ENUM), etc.
"""
with engine.begin() as conn:
inspector = sa.inspect(engine)
metadata = schema.MetaData()
tbs = []
all_fks = []
for table_name in inspector.get_table_names():
fks = []
for fk in inspector.get_foreign_keys(table_name):
if not fk["name"]:
continue
fks.append(
schema.ForeignKeyConstraint((), (), name=fk["name"]))
table = schema.Table(table_name, metadata, *fks)
tbs.append(table)
all_fks.extend(fks)
for fkc in all_fks:
conn.execute(schema.DropConstraint(fkc))
for table in tbs:
conn.execute(schema.DropTable(table))
if engine.name == "postgresql":
if compat_utils.sqla_100:
enums = [e["name"] for e in sa.inspect(conn).get_enums()]
else:
enums = conn.dialect._load_enums(conn).keys()
for e in enums:
conn.execute("DROP TYPE %s" % e)
def drop_db():
from rally.db.sqlalchemy import api as sa_api
engine = sa_api.get_engine()
OLD_BASE = declarative_base()
OLD_BASE.metadata.reflect(bind=engine)
OLD_BASE.metadata.drop_all(engine, checkfirst=True)
drop_all_objects(sa_api.get_engine())

View File

@ -18,6 +18,7 @@ pbr>=0.6,!=0.7,<1.0
pecan>=0.8.0
PrettyTable>=0.7,<0.8
PyYAML>=3.1.0
psycopg2
python-designateclient>=1.0.0
python-glanceclient>=0.15.0
python-keystoneclient>=0.11.1

View File

@ -31,6 +31,11 @@ To run py26, py27 or pep8 only::
#NOTE: <name> is one of py26, py27 or pep8
To run py26, py27 against mysql or psql
$ export RALLY_UNITTEST_DB_URL="mysql://user:secret@localhost/rally"
$ tox -epy27
To get test coverage::
$ tox -e cover

View File

@ -14,7 +14,6 @@
# under the License.
"""Tests for db.api layer."""
import uuid
from six import moves
@ -34,71 +33,70 @@ class TasksTestCase(test.DBTestCase):
def _create_task(self, values=None):
values = values or {}
if 'deployment_uuid' not in values:
values['deployment_uuid'] = self.deploy['uuid']
if "deployment_uuid" not in values:
values["deployment_uuid"] = self.deploy["uuid"]
return db.task_create(values)
def test_task_get_not_found(self):
self.assertRaises(exceptions.TaskNotFound,
db.task_get, 'f885f435-f6ca-4f3e-9b3e-aeb6837080f2')
db.task_get, "f885f435-f6ca-4f3e-9b3e-aeb6837080f2")
def test_task_create(self):
task = self._create_task()
db_task = self._get_task(task['uuid'])
self.assertIsNotNone(db_task['uuid'])
self.assertIsNotNone(db_task['id'])
self.assertEqual(db_task['status'], consts.TaskStatus.INIT)
db_task = self._get_task(task["uuid"])
self.assertIsNotNone(db_task["uuid"])
self.assertIsNotNone(db_task["id"])
self.assertEqual(db_task["status"], consts.TaskStatus.INIT)
def test_task_create_without_uuid(self):
_uuid = '19be8589-48b0-4af1-a369-9bebaaa563ab'
task = self._create_task({'uuid': _uuid})
db_task = self._get_task(task['uuid'])
self.assertEqual(db_task['uuid'], _uuid)
_uuid = "19be8589-48b0-4af1-a369-9bebaaa563ab"
task = self._create_task({"uuid": _uuid})
db_task = self._get_task(task["uuid"])
self.assertEqual(db_task["uuid"], _uuid)
def test_task_update(self):
task = self._create_task({})
db.task_update(task['uuid'], {'status': consts.TaskStatus.FAILED})
db_task = self._get_task(task['uuid'])
self.assertEqual(db_task['status'], consts.TaskStatus.FAILED)
db.task_update(task["uuid"], {"status": consts.TaskStatus.FAILED})
db_task = self._get_task(task["uuid"])
self.assertEqual(db_task["status"], consts.TaskStatus.FAILED)
def test_task_update_not_found(self):
self.assertRaises(exceptions.TaskNotFound,
db.task_update,
'7ae1da26-feaa-4213-8208-76af2857a5ab', {})
"7ae1da26-feaa-4213-8208-76af2857a5ab", {})
def test_task_update_all_stats(self):
_uuid = self._create_task({})['uuid']
_uuid = self._create_task({})["uuid"]
for status in consts.TaskStatus:
db.task_update(_uuid, {'status': status})
db.task_update(_uuid, {"status": status})
db_task = self._get_task(_uuid)
self.assertEqual(db_task['status'], status)
self.assertEqual(db_task["status"], status)
def test_task_list_empty(self):
self.assertEqual([], db.task_list())
def test_task_list(self):
fake_deploy_uuid = str(uuid.uuid4())
INIT = consts.TaskStatus.INIT
task_init = sorted(self._create_task()['uuid'] for i in moves.range(3))
task_init = sorted(self._create_task()["uuid"] for i in moves.range(3))
FINISHED = consts.TaskStatus.FINISHED
task_finished = sorted(self._create_task(
{'status': FINISHED,
'deployment_uuid': fake_deploy_uuid})[
'uuid']
{"status": FINISHED,
"deployment_uuid": self.deploy["uuid"]})[
"uuid"]
for i in moves.range(3))
task_all = sorted(task_init + task_finished)
def get_uuids(status=None, deployment=None):
tasks = db.task_list(status=status, deployment=deployment)
return sorted(task['uuid'] for task in tasks)
return sorted(task["uuid"] for task in tasks)
self.assertEqual(task_all, get_uuids(None))
self.assertEqual(task_init, get_uuids(status=INIT))
self.assertEqual(task_finished, get_uuids(status=FINISHED))
self.assertRaises(exceptions.DeploymentNotFound,
get_uuids, deployment=fake_deploy_uuid)
get_uuids, deployment="non-existing-deployment")
deleted_task_uuid = task_finished.pop()
db.task_delete(deleted_task_uuid)
@ -106,18 +104,18 @@ class TasksTestCase(test.DBTestCase):
self.assertEqual(sorted(task_finished), get_uuids(FINISHED))
def test_task_delete(self):
task1, task2 = self._create_task()['uuid'], self._create_task()['uuid']
task1, task2 = self._create_task()["uuid"], self._create_task()["uuid"]
db.task_delete(task1)
self.assertRaises(exceptions.TaskNotFound, self._get_task, task1)
self.assertEqual(task2, self._get_task(task2)['uuid'])
self.assertEqual(task2, self._get_task(task2)["uuid"])
def test_task_delete_not_found(self):
self.assertRaises(exceptions.TaskNotFound,
db.task_delete,
'da6f820c-b133-4b9f-8534-4c3bcc40724b')
"da6f820c-b133-4b9f-8534-4c3bcc40724b")
def test_task_delete_with_results(self):
task_id = self._create_task()['uuid']
task_id = self._create_task()["uuid"]
db.task_result_create(task_id,
{task_id: task_id},
{task_id: task_id})
@ -129,30 +127,29 @@ class TasksTestCase(test.DBTestCase):
def test_task_delete_by_uuid_and_status(self):
values = {
'status': consts.TaskStatus.FINISHED,
"status": consts.TaskStatus.FINISHED,
}
task1 = self._create_task(values=values)['uuid']
task2 = self._create_task(values=values)['uuid']
task1 = self._create_task(values=values)["uuid"]
task2 = self._create_task(values=values)["uuid"]
db.task_delete(task1, status=consts.TaskStatus.FINISHED)
self.assertRaises(exceptions.TaskNotFound, self._get_task, task1)
self.assertEqual(task2, self._get_task(task2)['uuid'])
self.assertEqual(task2, self._get_task(task2)["uuid"])
def test_task_delete_by_uuid_and_status_invalid(self):
task = self._create_task(values={
'status': consts.TaskStatus.INIT,
})['uuid']
task = self._create_task(
values={"status": consts.TaskStatus.INIT})["uuid"]
self.assertRaises(exceptions.TaskInvalidStatus, db.task_delete, task,
status=consts.TaskStatus.FINISHED)
def test_task_delete_by_uuid_and_status_not_found(self):
self.assertRaises(exceptions.TaskNotFound,
db.task_delete,
'fcd0483f-a405-44c4-b712-99c9e52254eb',
"fcd0483f-a405-44c4-b712-99c9e52254eb",
status=consts.TaskStatus.FINISHED)
def test_task_result_get_all_by_uuid(self):
task1 = self._create_task()['uuid']
task2 = self._create_task()['uuid']
task1 = self._create_task()["uuid"]
task2 = self._create_task()["uuid"]
for task_id in (task1, task2):
db.task_result_create(task_id,
@ -163,16 +160,16 @@ class TasksTestCase(test.DBTestCase):
res = db.task_result_get_all_by_uuid(task_id)
data = {task_id: task_id}
self.assertEqual(len(res), 1)
self.assertEqual(res[0]['key'], data)
self.assertEqual(res[0]['data'], data)
self.assertEqual(res[0]["key"], data)
self.assertEqual(res[0]["data"], data)
def test_task_get_detailed(self):
task1 = self._create_task()
key = {'name': 'atata'}
data = {'a': 'b', 'c': 'd'}
key = {"name": "atata"}
data = {"a": "b", "c": "d"}
db.task_result_create(task1['uuid'], key, data)
task1_full = db.task_get_detailed(task1['uuid'])
db.task_result_create(task1["uuid"], key, data)
task1_full = db.task_get_detailed(task1["uuid"])
results = task1_full["results"]
self.assertEqual(len(results), 1)
self.assertEqual(results[0]["key"], key)
@ -180,10 +177,10 @@ class TasksTestCase(test.DBTestCase):
def test_task_get_detailed_last(self):
task1 = self._create_task()
key = {'name': 'atata'}
data = {'a': 'b', 'c': 'd'}
key = {"name": "atata"}
data = {"a": "b", "c": "d"}
db.task_result_create(task1['uuid'], key, data)
db.task_result_create(task1["uuid"], key, data)
task1_full = db.task_get_detailed_last()
results = task1_full["results"]
self.assertEqual(len(results), 1)
@ -193,116 +190,113 @@ class TasksTestCase(test.DBTestCase):
class DeploymentTestCase(test.DBTestCase):
def test_deployment_create(self):
deploy = db.deployment_create({'config': {'opt': 'val'}})
deploy = db.deployment_create({"config": {"opt": "val"}})
deploys = db.deployment_list()
self.assertEqual(len(deploys), 1)
self.assertEqual(deploy['uuid'], deploys[0]['uuid'])
self.assertEqual(deploy['status'], consts.DeployStatus.DEPLOY_INIT)
self.assertEqual(deploy['config'], {'opt': 'val'})
self.assertEqual(deploy["uuid"], deploys[0]["uuid"])
self.assertEqual(deploy["status"], consts.DeployStatus.DEPLOY_INIT)
self.assertEqual(deploy["config"], {"opt": "val"})
def test_deployment_create_several(self):
# Create a deployment
deploys = db.deployment_list()
self.assertEqual(len(deploys), 0)
deploy_one = db.deployment_create({'config': {'opt1': 'val1'}})
deploy_one = db.deployment_create({"config": {"opt1": "val1"}})
deploys = db.deployment_list()
self.assertEqual(len(deploys), 1)
self.assertEqual(deploy_one['uuid'], deploys[0]['uuid'])
self.assertEqual(deploy_one['status'], consts.DeployStatus.DEPLOY_INIT)
self.assertEqual(deploy_one['config'], {'opt1': 'val1'})
self.assertEqual(deploy_one["uuid"], deploys[0]["uuid"])
self.assertEqual(deploy_one["status"], consts.DeployStatus.DEPLOY_INIT)
self.assertEqual(deploy_one["config"], {"opt1": "val1"})
# Create another deployment and sure that they are different
deploy_two = db.deployment_create({'config': {'opt2': 'val2'}})
deploy_two = db.deployment_create({"config": {"opt2": "val2"}})
deploys = db.deployment_list()
self.assertEqual(len(deploys), 2)
self.assertEqual([deploy_one['uuid'], deploy_two['uuid']],
[deploy['uuid'] for deploy in deploys])
self.assertNotEqual(deploy_one['uuid'], deploy_two['uuid'])
self.assertEqual(deploy_two['status'], consts.DeployStatus.DEPLOY_INIT)
self.assertEqual(deploy_two['config'], {'opt2': 'val2'})
self.assertEqual(set([deploy_one["uuid"], deploy_two["uuid"]]),
set([deploy["uuid"] for deploy in deploys]))
self.assertNotEqual(deploy_one["uuid"], deploy_two["uuid"])
self.assertEqual(deploy_two["status"], consts.DeployStatus.DEPLOY_INIT)
self.assertEqual(deploy_two["config"], {"opt2": "val2"})
def test_deployment_update(self):
deploy = db.deployment_create({})
self.assertEqual(deploy['config'], {})
update_deploy = db.deployment_update(deploy['uuid'],
{'config': {'opt': 'val'}})
self.assertEqual(update_deploy['uuid'], deploy['uuid'])
self.assertEqual(update_deploy['config'], {'opt': 'val'})
get_deploy = db.deployment_get(deploy['uuid'])
self.assertEqual(get_deploy['uuid'], deploy['uuid'])
self.assertEqual(get_deploy['config'], {'opt': 'val'})
self.assertEqual(deploy["config"], {})
update_deploy = db.deployment_update(deploy["uuid"],
{"config": {"opt": "val"}})
self.assertEqual(update_deploy["uuid"], deploy["uuid"])
self.assertEqual(update_deploy["config"], {"opt": "val"})
get_deploy = db.deployment_get(deploy["uuid"])
self.assertEqual(get_deploy["uuid"], deploy["uuid"])
self.assertEqual(get_deploy["config"], {"opt": "val"})
def test_deployment_update_several(self):
# Create a deployment and update it
deploy_one = db.deployment_create({})
self.assertEqual(deploy_one['config'], {})
self.assertEqual(deploy_one["config"], {})
update_deploy_one = db.deployment_update(
deploy_one['uuid'],
{'config': {'opt1': 'val1'}},
)
self.assertEqual(update_deploy_one['uuid'], deploy_one['uuid'])
self.assertEqual(update_deploy_one['config'], {'opt1': 'val1'})
get_deploy_one = db.deployment_get(deploy_one['uuid'])
self.assertEqual(get_deploy_one['uuid'], deploy_one['uuid'])
self.assertEqual(get_deploy_one['config'], {'opt1': 'val1'})
deploy_one["uuid"], {"config": {"opt1": "val1"}})
self.assertEqual(update_deploy_one["uuid"], deploy_one["uuid"])
self.assertEqual(update_deploy_one["config"], {"opt1": "val1"})
get_deploy_one = db.deployment_get(deploy_one["uuid"])
self.assertEqual(get_deploy_one["uuid"], deploy_one["uuid"])
self.assertEqual(get_deploy_one["config"], {"opt1": "val1"})
# Create another deployment
deploy_two = db.deployment_create({})
update_deploy_two = db.deployment_update(
deploy_two['uuid'],
{'config': {'opt2': 'val2'}},
)
self.assertEqual(update_deploy_two['uuid'], deploy_two['uuid'])
self.assertEqual(update_deploy_two['config'], {'opt2': 'val2'})
get_deploy_one_again = db.deployment_get(deploy_one['uuid'])
self.assertEqual(get_deploy_one_again['uuid'], deploy_one['uuid'])
self.assertEqual(get_deploy_one_again['config'], {'opt1': 'val1'})
deploy_two["uuid"], {"config": {"opt2": "val2"}})
self.assertEqual(update_deploy_two["uuid"], deploy_two["uuid"])
self.assertEqual(update_deploy_two["config"], {"opt2": "val2"})
get_deploy_one_again = db.deployment_get(deploy_one["uuid"])
self.assertEqual(get_deploy_one_again["uuid"], deploy_one["uuid"])
self.assertEqual(get_deploy_one_again["config"], {"opt1": "val1"})
def test_deployment_get(self):
deploy_one = db.deployment_create({'config': {'opt1': 'val1'}})
deploy_two = db.deployment_create({'config': {'opt2': 'val2'}})
get_deploy_one = db.deployment_get(deploy_one['uuid'])
get_deploy_two = db.deployment_get(deploy_two['uuid'])
self.assertNotEqual(get_deploy_one['uuid'], get_deploy_two['uuid'])
self.assertEqual(get_deploy_one['config'], {'opt1': 'val1'})
self.assertEqual(get_deploy_two['config'], {'opt2': 'val2'})
deploy_one = db.deployment_create({"config": {"opt1": "val1"}})
deploy_two = db.deployment_create({"config": {"opt2": "val2"}})
get_deploy_one = db.deployment_get(deploy_one["uuid"])
get_deploy_two = db.deployment_get(deploy_two["uuid"])
self.assertNotEqual(get_deploy_one["uuid"], get_deploy_two["uuid"])
self.assertEqual(get_deploy_one["config"], {"opt1": "val1"})
self.assertEqual(get_deploy_two["config"], {"opt2": "val2"})
def test_deployment_get_not_found(self):
self.assertRaises(exceptions.DeploymentNotFound,
db.deployment_get,
'852e932b-9552-4b2d-89e3-a5915780a5e3')
"852e932b-9552-4b2d-89e3-a5915780a5e3")
def test_deployment_list(self):
deploy_one = db.deployment_create({})
deploy_two = db.deployment_create({})
deploys = db.deployment_list()
self.assertEqual(sorted([deploy_one['uuid'], deploy_two['uuid']]),
sorted([deploy['uuid'] for deploy in deploys]))
self.assertEqual(sorted([deploy_one["uuid"], deploy_two["uuid"]]),
sorted([deploy["uuid"] for deploy in deploys]))
def test_deployment_list_with_status_and_name(self):
deploy_one = db.deployment_create({})
deploy_two = db.deployment_create({
'config': {},
'status': consts.DeployStatus.DEPLOY_FAILED,
"config": {},
"status": consts.DeployStatus.DEPLOY_FAILED,
})
deploy_three = db.deployment_create({'name': 'deployment_name'})
deploy_three = db.deployment_create({"name": "deployment_name"})
deploys = db.deployment_list(status=consts.DeployStatus.DEPLOY_INIT)
deploys.sort(key=lambda x: x["id"])
self.assertEqual(len(deploys), 2)
self.assertEqual(deploys[0]['uuid'], deploy_one['uuid'])
self.assertEqual(deploys[0]["uuid"], deploy_one["uuid"])
deploys = db.deployment_list(status=consts.DeployStatus.DEPLOY_FAILED)
self.assertEqual(len(deploys), 1)
self.assertEqual(deploys[0]['uuid'], deploy_two['uuid'])
self.assertEqual(deploys[0]["uuid"], deploy_two["uuid"])
deploys = db.deployment_list(
status=consts.DeployStatus.DEPLOY_FINISHED)
self.assertEqual(len(deploys), 0)
deploys = db.deployment_list(name='deployment_name')
self.assertEqual(deploys[0]['uuid'], deploy_three['uuid'])
deploys = db.deployment_list(name="deployment_name")
self.assertEqual(deploys[0]["uuid"], deploy_three["uuid"])
self.assertEqual(len(deploys), 1)
def test_deployment_list_parent(self):
deploy = db.deployment_create({})
subdeploy1 = db.deployment_create({'parent_uuid': deploy.uuid})
subdeploy2 = db.deployment_create({'parent_uuid': deploy.uuid})
subdeploy1 = db.deployment_create({"parent_uuid": deploy.uuid})
subdeploy2 = db.deployment_create({"parent_uuid": deploy.uuid})
self.assertEqual([deploy.uuid], [d.uuid for d in db.deployment_list()])
subdeploys = db.deployment_list(parent_uuid=deploy.uuid)
self.assertEqual(set([subdeploy1.uuid, subdeploy2.uuid]),
@ -311,102 +305,99 @@ class DeploymentTestCase(test.DBTestCase):
def test_deployment_delete(self):
deploy_one = db.deployment_create({})
deploy_two = db.deployment_create({})
db.deployment_delete(deploy_two['uuid'])
db.deployment_delete(deploy_two["uuid"])
deploys = db.deployment_list()
self.assertEqual(len(deploys), 1)
self.assertEqual(deploys[0]['uuid'], deploy_one['uuid'])
self.assertEqual(deploys[0]["uuid"], deploy_one["uuid"])
def test_deployment_delete_not_found(self):
self.assertRaises(exceptions.DeploymentNotFound,
db.deployment_delete,
'5f2883be-46c8-4c4b-a4fe-988ad0c6b20a')
"5f2883be-46c8-4c4b-a4fe-988ad0c6b20a")
def test_deployment_delete_is_busy(self):
deployment = db.deployment_create({})
db.resource_create({'deployment_uuid': deployment['uuid']})
db.resource_create({'deployment_uuid': deployment['uuid']})
db.resource_create({"deployment_uuid": deployment["uuid"]})
db.resource_create({"deployment_uuid": deployment["uuid"]})
self.assertRaises(exceptions.DeploymentIsBusy, db.deployment_delete,
deployment['uuid'])
deployment["uuid"])
class ResourceTestCase(test.DBTestCase):
def test_create(self):
deployment = db.deployment_create({})
resource = db.resource_create({
'deployment_uuid': deployment['uuid'],
'provider_name': 'fakeprovider',
'type': 'faketype',
"deployment_uuid": deployment["uuid"],
"provider_name": "fakeprovider",
"type": "faketype",
})
resources = db.resource_get_all(deployment['uuid'])
self.assertTrue(resource['id'])
resources = db.resource_get_all(deployment["uuid"])
self.assertTrue(resource["id"])
self.assertEqual(len(resources), 1)
self.assertTrue(resource['id'], resources[0]['id'])
self.assertEqual(resource['deployment_uuid'], deployment['uuid'])
self.assertEqual(resource['provider_name'], 'fakeprovider')
self.assertEqual(resource['type'], 'faketype')
self.assertTrue(resource["id"], resources[0]["id"])
self.assertEqual(resource["deployment_uuid"], deployment["uuid"])
self.assertEqual(resource["provider_name"], "fakeprovider")
self.assertEqual(resource["type"], "faketype")
def test_delete(self):
deployment = db.deployment_create({})
res = db.resource_create({'deployment_uuid': deployment['uuid']})
db.resource_delete(res['id'])
resources = db.resource_get_all(deployment['id'])
res = db.resource_create({"deployment_uuid": deployment["uuid"]})
db.resource_delete(res["id"])
resources = db.resource_get_all(deployment["uuid"])
self.assertEqual(len(resources), 0)
def test_delete_not_found(self):
self.assertRaises(exceptions.ResourceNotFound,
db.resource_delete,
'2d02ecb0-d973-4ffd-975d-2f08a906222b')
db.resource_delete, 123456789)
def test_get_all(self):
deployment0 = db.deployment_create({})
deployment1 = db.deployment_create({})
res0 = db.resource_create({'deployment_uuid': deployment0['uuid']})
res1 = db.resource_create({'deployment_uuid': deployment1['uuid']})
res2 = db.resource_create({'deployment_uuid': deployment1['uuid']})
resources = db.resource_get_all(deployment1['uuid'])
self.assertEqual(sorted([res1['id'], res2['id']]),
sorted([r['id'] for r in resources]))
resources = db.resource_get_all(deployment0['uuid'])
res0 = db.resource_create({"deployment_uuid": deployment0["uuid"]})
res1 = db.resource_create({"deployment_uuid": deployment1["uuid"]})
res2 = db.resource_create({"deployment_uuid": deployment1["uuid"]})
resources = db.resource_get_all(deployment1["uuid"])
self.assertEqual(sorted([res1["id"], res2["id"]]),
sorted([r["id"] for r in resources]))
resources = db.resource_get_all(deployment0["uuid"])
self.assertEqual(len(resources), 1)
self.assertEqual(res0['id'], resources[0]['id'])
self.assertEqual(res0["id"], resources[0]["id"])
def test_get_all_by_provider_name(self):
deployment = db.deployment_create({})
res_one = db.resource_create({
'deployment_uuid': deployment['uuid'],
'provider_name': 'one',
"deployment_uuid": deployment["uuid"],
"provider_name": "one",
})
res_two = db.resource_create({
'deployment_uuid': deployment['uuid'],
'provider_name': 'two',
"deployment_uuid": deployment["uuid"],
"provider_name": "two",
})
resources = db.resource_get_all(deployment['uuid'],
provider_name='one')
resources = db.resource_get_all(deployment["uuid"],
provider_name="one")
self.assertEqual(len(resources), 1)
self.assertEqual(res_one['id'], resources[0]['id'])
resources = db.resource_get_all(deployment['uuid'],
provider_name='two')
self.assertEqual(res_one["id"], resources[0]["id"])
resources = db.resource_get_all(deployment["uuid"],
provider_name="two")
self.assertEqual(len(resources), 1)
self.assertEqual(res_two['id'], resources[0]['id'])
self.assertEqual(res_two["id"], resources[0]["id"])
def test_get_all_by_provider_type(self):
deployment = db.deployment_create({})
res_one = db.resource_create({
'deployment_uuid': deployment['uuid'],
'type': 'one',
"deployment_uuid": deployment["uuid"],
"type": "one",
})
res_two = db.resource_create({
'deployment_uuid': deployment['uuid'],
'type': 'two',
"deployment_uuid": deployment["uuid"],
"type": "two",
})
resources = db.resource_get_all(deployment['uuid'],
type='one')
resources = db.resource_get_all(deployment["uuid"], type="one")
self.assertEqual(len(resources), 1)
self.assertEqual(res_one['id'], resources[0]['id'])
resources = db.resource_get_all(deployment['uuid'],
type='two')
self.assertEqual(res_one["id"], resources[0]["id"])
resources = db.resource_get_all(deployment["uuid"], type="two")
self.assertEqual(len(resources), 1)
self.assertEqual(res_two['id'], resources[0]['id'])
self.assertEqual(res_two["id"], resources[0]["id"])
class VerificationTestCase(test.DBTestCase):
@ -415,51 +406,48 @@ class VerificationTestCase(test.DBTestCase):
self.deploy = db.deployment_create({})
def _create_verification(self):
deployment_uuid = self.deploy['uuid']
deployment_uuid = self.deploy["uuid"]
return db.verification_create(deployment_uuid)
def test_creation_of_verification(self):
verification = self._create_verification()
db_verification = db.verification_get(verification['uuid'])
db_verification = db.verification_get(verification["uuid"])
self.assertEqual(verification['tests'], db_verification['tests'])
self.assertEqual(verification['time'], db_verification['time'])
self.assertEqual(verification['errors'], db_verification['errors'])
self.assertEqual(verification['failures'], db_verification['failures'])
self.assertEqual(verification["tests"], db_verification["tests"])
self.assertEqual(verification["time"], db_verification["time"])
self.assertEqual(verification["errors"], db_verification["errors"])
self.assertEqual(verification["failures"], db_verification["failures"])
class WorkerTestCase(test.DBTestCase):
def setUp(self):
super(WorkerTestCase, self).setUp()
self.worker = db.register_worker({'hostname': 'test'})
self.worker = db.register_worker({"hostname": "test"})
def test_register_worker_duplicate(self):
self.assertRaises(exceptions.WorkerAlreadyRegistered,
db.register_worker, {'hostname': 'test'})
db.register_worker, {"hostname": "test"})
def test_get_worker(self):
worker = db.get_worker('test')
self.assertEqual(self.worker['id'], worker['id'])
self.assertEqual(self.worker['hostname'], worker['hostname'])
worker = db.get_worker("test")
self.assertEqual(self.worker["id"], worker["id"])
self.assertEqual(self.worker["hostname"], worker["hostname"])
def test_get_worker_not_found(self):
self.assertRaises(exceptions.WorkerNotFound,
db.get_worker, 'notfound')
self.assertRaises(exceptions.WorkerNotFound, db.get_worker, "notfound")
def test_unregister_worker(self):
db.unregister_worker('test')
self.assertRaises(exceptions.WorkerNotFound,
db.get_worker, 'test')
db.unregister_worker("test")
self.assertRaises(exceptions.WorkerNotFound, db.get_worker, "test")
def test_unregister_worker_not_found(self):
self.assertRaises(exceptions.WorkerNotFound,
db.unregister_worker, 'fake')
db.unregister_worker, "fake")
def test_update_worker(self):
db.update_worker('test')
worker = db.get_worker('test')
self.assertNotEqual(self.worker['updated_at'], worker['updated_at'])
db.update_worker("test")
worker = db.get_worker("test")
self.assertNotEqual(self.worker["updated_at"], worker["updated_at"])
def test_update_worker_not_found(self):
self.assertRaises(exceptions.WorkerNotFound,
db.update_worker, 'fake')
self.assertRaises(exceptions.WorkerNotFound, db.update_worker, "fake")

View File

@ -13,6 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import os
import mock
from oslo.config import fixture
from oslotest import base
@ -24,8 +26,9 @@ class DatabaseFixture(fixture.Config):
"""Create clean DB before starting test."""
def setUp(self):
super(DatabaseFixture, self).setUp()
db_url = os.environ.get("RALLY_UNITTEST_DB_URL", "sqlite://")
db.db_cleanup()
self.conf.set_default('connection', "sqlite://", group='database')
self.conf.set_default("connection", db_url, group="database")
db.db_drop()
db.db_create()