2016-08-08 09:12:21 -06:00
|
|
|
# (C) Copyright 2015 Hewlett Packard Enterprise Development LP
|
2017-05-16 00:14:56 +02:00
|
|
|
# Copyright 2017 Fujitsu LIMITED
|
2015-08-21 14:37:53 -06:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2020-04-18 11:55:01 -05:00
|
|
|
from unittest import mock
|
2015-08-21 14:37:53 -06:00
|
|
|
|
2015-10-06 08:56:11 +02:00
|
|
|
import pymysql
|
2015-08-21 14:37:53 -06:00
|
|
|
|
|
|
|
from monasca_notification.common.repositories import exceptions as exc
|
|
|
|
from monasca_notification.common.repositories.mysql import mysql_repo
|
2017-05-16 00:14:56 +02:00
|
|
|
from tests import base
|
2015-08-21 14:37:53 -06:00
|
|
|
|
|
|
|
|
2017-05-16 00:14:56 +02:00
|
|
|
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')
|
|
|
|
|
2015-10-06 08:56:11 +02:00
|
|
|
@mock.patch('monasca_notification.common.repositories.mysql.mysql_repo.pymysql')
|
2015-08-21 14:37:53 -06:00
|
|
|
def testReconnect(self, mock_mysql):
|
|
|
|
m = mock.MagicMock()
|
|
|
|
|
2015-10-06 08:56:11 +02:00
|
|
|
m.cursor.side_effect = pymysql.Error
|
2015-08-21 14:37:53 -06:00
|
|
|
|
|
|
|
mock_mysql.connect.return_value = m
|
2015-10-06 08:56:11 +02:00
|
|
|
mock_mysql.Error = pymysql.Error
|
2015-08-21 14:37:53 -06:00
|
|
|
|
2017-05-16 00:14:56 +02:00
|
|
|
repo = mysql_repo.MysqlRepo(base.config.CONF)
|
2015-08-21 14:37:53 -06:00
|
|
|
|
|
|
|
alarm = {'alarmDefinitionId': 'foo',
|
|
|
|
'newState': 'bar'}
|
|
|
|
|
|
|
|
def get_notification(repo, alarm):
|
2016-08-08 09:12:21 -06:00
|
|
|
g = repo.fetch_notifications(alarm)
|
2015-08-21 14:37:53 -06:00
|
|
|
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)
|