monasca-notification/tests/test_mysql_repo.py

61 lines
1.9 KiB
Python

# (C) Copyright 2015 Hewlett Packard Enterprise Development LP
# Copyright 2017 Fujitsu LIMITED
#
# 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.
import mock
import pymysql
from monasca_notification.common.repositories import exceptions as exc
from monasca_notification.common.repositories.mysql import mysql_repo
from tests import base
class TestMySqlRepo(base.BaseTestCase):
def setUp(self):
super(TestMySqlRepo, self).setUp()
self.conf_default(group='mysql', host='localhost',
port=3306, user='bar',
passwd='1', db='2')
@mock.patch('monasca_notification.common.repositories.mysql.mysql_repo.pymysql')
def testReconnect(self, mock_mysql):
m = mock.MagicMock()
m.cursor.side_effect = pymysql.Error
mock_mysql.connect.return_value = m
mock_mysql.Error = pymysql.Error
repo = mysql_repo.MysqlRepo(base.config.CONF)
alarm = {'alarmDefinitionId': 'foo',
'newState': 'bar'}
def get_notification(repo, alarm):
g = repo.fetch_notifications(alarm)
for x in g:
pass
try:
get_notification(repo, alarm)
except exc.DatabaseException:
try:
get_notification(repo, alarm)
except Exception:
pass
self.assertEqual(mock_mysql.connect.call_count, 2)