Add test case to create alarm into database

Add test class DbAlarmTestCase, DbTestCase
Add fake database in tests/base.py.
Add test case test_create_alarm

Story: 2007082
Task: 38085

Depends-on: https://review.opendev.org/#/c/702175/
Depends-on: https://review.opendev.org/#/c/702347/
Change-Id: I906edc894c97fdeb8c52730e00fba765d33edc0f
Signed-off-by: SidneyAn <ran1.an@intel.com>
This commit is contained in:
SidneyAn 2020-01-16 16:46:44 +08:00
parent d73ba87457
commit 170d4fefb7
6 changed files with 169 additions and 0 deletions

View File

@ -26,9 +26,15 @@ import mock
import testtools import testtools
from oslo_config import cfg from oslo_config import cfg
from oslo_db.sqlalchemy import enginefacade
from oslo_log import log as logging from oslo_log import log as logging
from fm.db import migration
from fm.tests import conf_fixture
CONF = cfg.CONF CONF = cfg.CONF
_DB_CACHE = None
INIT_VERSION = 0
sys.modules['fm_core'] = mock.Mock() sys.modules['fm_core'] = mock.Mock()
@ -47,5 +53,14 @@ class TestCase(testtools.TestCase):
fixtures.MonkeyPatch('oslo_log.log.setup', fake_logging_setup)) fixtures.MonkeyPatch('oslo_log.log.setup', fake_logging_setup))
logging.register_options(CONF) 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): def tearDown(self):
super(TestCase, self).tearDown() super(TestCase, self).tearDown()

View File

@ -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)

View File

@ -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.

View File

@ -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)

View File

@ -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)

View File

@ -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)