diff --git a/fm-rest-api/fm/fm/tests/base.py b/fm-rest-api/fm/fm/tests/base.py index 3f5e7e71..e6abb5d2 100644 --- a/fm-rest-api/fm/fm/tests/base.py +++ b/fm-rest-api/fm/fm/tests/base.py @@ -26,9 +26,15 @@ import mock import testtools from oslo_config import cfg +from oslo_db.sqlalchemy import enginefacade from oslo_log import log as logging +from fm.db import migration +from fm.tests import conf_fixture + CONF = cfg.CONF +_DB_CACHE = None +INIT_VERSION = 0 sys.modules['fm_core'] = mock.Mock() @@ -47,5 +53,14 @@ class TestCase(testtools.TestCase): fixtures.MonkeyPatch('oslo_log.log.setup', fake_logging_setup)) logging.register_options(CONF) + self.useFixture(conf_fixture.ConfFixture(CONF)) + + global _DB_CACHE + if not _DB_CACHE: + engine = enginefacade.get_legacy_facade().get_engine() + engine.dispose() + engine.connect() + migration.db_sync(engine=engine) + def tearDown(self): super(TestCase, self).tearDown() diff --git a/fm-rest-api/fm/fm/tests/conf_fixture.py b/fm-rest-api/fm/fm/tests/conf_fixture.py new file mode 100644 index 00000000..5440ee87 --- /dev/null +++ b/fm-rest-api/fm/fm/tests/conf_fixture.py @@ -0,0 +1,33 @@ +# Copyright 2010 United States Government as represented by the +# Administrator of the National Aeronautics and Space Administration. +# All Rights Reserved. +# +# 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 oslo_config import cfg +from oslo_config import fixture as config_fixture + +CONF = cfg.CONF + + +class ConfFixture(config_fixture.Config): + """Fixture to manage global conf settings.""" + + def __init__(self, conf): + self.conf = conf + + def setUp(self): + super(ConfFixture, self).setUp() + + self.conf.set_default('connection', "sqlite://", group='database') + self.addCleanup(self.conf.reset) diff --git a/fm-rest-api/fm/fm/tests/db/__init__.py b/fm-rest-api/fm/fm/tests/db/__init__.py new file mode 100644 index 00000000..70618e64 --- /dev/null +++ b/fm-rest-api/fm/fm/tests/db/__init__.py @@ -0,0 +1,14 @@ +# Copyright 2020 Intel Corporation. +# All Rights Reserved. +# +# 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. diff --git a/fm-rest-api/fm/fm/tests/db/base.py b/fm-rest-api/fm/fm/tests/db/base.py new file mode 100644 index 00000000..43131988 --- /dev/null +++ b/fm-rest-api/fm/fm/tests/db/base.py @@ -0,0 +1,30 @@ +# Copyright 2020 Intel Corporation. +# All Rights Reserved. +# +# 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. + +"""Fault DB test base class.""" + +import abc +import six + +from fm.common import context +from fm.tests import base + + +@six.add_metaclass(abc.ABCMeta) +class DbTestCase(base.TestCase): + + def setUp(self): + super(DbTestCase, self).setUp() + self.admin_context = context.make_context(is_admin=True) diff --git a/fm-rest-api/fm/fm/tests/db/test_alarm.py b/fm-rest-api/fm/fm/tests/db/test_alarm.py new file mode 100644 index 00000000..3c64c471 --- /dev/null +++ b/fm-rest-api/fm/fm/tests/db/test_alarm.py @@ -0,0 +1,24 @@ +# Copyright 2020 Intel Corporation. +# +# SPDX-License-Identifier: Apache-2.0 +# + + +"""Tests for Alarm via the DB API""" + +from fm.db import api as dbapi +from fm.tests.db import base +from fm.tests.db import utils + + +class DbAlarmTestCase(base.DbTestCase): + + def setUp(self): + super(DbAlarmTestCase, self).setUp() + self.dbapi = dbapi.get_instance() + + def test_create_alarm(self): + uuid = 1234567 + alarm = utils.get_test_alarm(uuid=uuid) + alarm_exist = self.dbapi.alarm_create(alarm) + self.assertEqual(uuid, alarm_exist.uuid) diff --git a/fm-rest-api/fm/fm/tests/db/utils.py b/fm-rest-api/fm/fm/tests/db/utils.py new file mode 100644 index 00000000..94153b59 --- /dev/null +++ b/fm-rest-api/fm/fm/tests/db/utils.py @@ -0,0 +1,53 @@ +# All Rights Reserved. +# +# 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. +# +# Copyright 2020 Intel Corporation. +# + +"""Fault test utilities.""" + +from fm.db import api as db_api +from fm_api import constants + + +def get_test_alarm(**kw): + alarm = { + 'uuid': kw.get('uuid'), + 'alarm_id': kw.get('alarm_id', constants.FM_ALARM_ID_VM_FAILED), + 'alarm_state': kw.get('alarm_state', constants.FM_ALARM_STATE_SET), + 'entity_type_id': kw.get('entity_type_id', constants.FM_ENTITY_TYPE_INSTANCE), + 'entity_instance_id': kw.get('entity_instance_id', + constants.FM_ENTITY_TYPE_INSTANCE + '=' + + 'a4e4cdb7-2ee6-4818-84c8-5310fcd67b5d'), + 'severity': kw.get('severity', constants.FM_ALARM_SEVERITY_CRITICAL), + 'reason_text': kw.get('reason_text', "Unknown"), + 'alarm_type': kw.get('alarm_type', constants.FM_ALARM_TYPE_5), + 'probable_cause': kw.get('probable_cause', constants.ALARM_PROBABLE_CAUSE_8), + 'proposed_repair_action': None, + 'service_affecting': False, + 'suppression': False + } + return alarm + + +def create_test_alarm(**kw): + """Create test alarm entry in DB and return alarm DB object. + Function to be used to create test alarm objects in the database. + :param kw: kwargs with overriding values for alarm's attributes. + :returns: Test alarm DB object. + """ + alarm = get_test_alarm(**kw) + # Let DB generate ID if it isn't specified explicitly + dbapi = db_api.get_instance() + return dbapi.alarm_create(alarm)