| 1 |
# Copyright(c) 2017 Nippon Telegraph and Telephone Corporation |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
import testtools |
| 16 |
from unittest import mock |
| 17 |
import uuid |
| 18 |
|
| 19 |
import eventlet |
| 20 |
from keystoneauth1 import loading as ks_loading |
| 21 |
from openstack import connection |
| 22 |
from openstack import exceptions |
| 23 |
from oslo_utils import timeutils |
| 24 |
|
| 25 |
import masakarimonitors.conf |
| 26 |
from masakarimonitors.ha import masakari |
| 27 |
from masakarimonitors.objects import event_constants as ec |
| 28 |
|
| 29 |
CONF = masakarimonitors.conf.CONF |
| 30 |
|
| 31 |
PROFILE_TYPE = "ha" |
| 32 |
PROFILE_NAME = "masakari" |
| 33 |
|
| 34 |
|
| 35 |
class FakeResponse(object): |
| 36 |
|
| 37 |
def __init__(self, status_code=200, headers=None): |
| 38 |
self.status_code = status_code |
| 39 |
self.headers = { |
| 40 |
'content-type': 'application/json', |
| 41 |
'x-openstack-request-id': uuid.uuid4().hex, |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
class TestSendNotification(testtools.TestCase): |
| 46 |
|
| 47 |
def setUp(self): |
| 48 |
super(TestSendNotification, self).setUp() |
| 49 |
self.api_retry_max = 3 |
| 50 |
self.api_retry_interval = 1 |
| 51 |
self.event = { |
| 52 |
'notification': { |
| 53 |
'type': ec.EventConstants.TYPE_COMPUTE_HOST, |
| 54 |
'hostname': 'compute-node1', |
| 55 |
'generated_time': timeutils.utcnow(), |
| 56 |
'payload': { |
| 57 |
'event': ec.EventConstants.EVENT_STOPPED, |
| 58 |
'cluster_status': 'OFFLINE', |
| 59 |
'host_status': ec.EventConstants.HOST_STATUS_NORMAL |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
@mock.patch.object(connection, 'Connection') |
| 65 |
@mock.patch.object(ks_loading, 'load_session_from_conf_options') |
| 66 |
@mock.patch.object(ks_loading, 'load_auth_from_conf_options') |
| 67 |
def test_send_notification( |
| 68 |
self, mock_auth, mock_session, mock_connection): |
| 69 |
|
| 70 |
mock_conn = mock.Mock() |
| 71 |
mock_conn.instance_ha.return_value = mock.Mock() |
| 72 |
mock_conn.instance_ha.create_notification.return_value = mock.Mock() |
| 73 |
mock_connection.return_value = mock_conn |
| 74 |
|
| 75 |
notifier = masakari.SendNotification() |
| 76 |
notifier.send_notification( |
| 77 |
self.api_retry_max, self.api_retry_interval, self.event) |
| 78 |
|
| 79 |
mock_conn.instance_ha.create_notification.assert_called_once_with( |
| 80 |
type=self.event['notification']['type'], |
| 81 |
hostname=self.event['notification']['hostname'], |
| 82 |
generated_time=self.event['notification']['generated_time'], |
| 83 |
payload=self.event['notification']['payload']) |
| 84 |
|
| 85 |
mock_auth.assert_called_once_with(CONF, 'api') |
| 86 |
mock_session.assert_called_once_with(CONF, 'api', |
| 87 |
auth=mock_auth.return_value) |
| 88 |
|
| 89 |
@mock.patch.object(connection, 'Connection') |
| 90 |
@mock.patch.object(ks_loading, 'load_session_from_conf_options') |
| 91 |
@mock.patch.object(ks_loading, 'load_auth_from_conf_options') |
| 92 |
def test_send_notification_409_error( |
| 93 |
self, mock_auth, mock_session, mock_connection): |
| 94 |
|
| 95 |
mock_conn = mock.Mock() |
| 96 |
mock_conn.instance_ha.return_value = mock.Mock() |
| 97 |
mock_conn.instance_ha.create_notification.return_value = mock.Mock() |
| 98 |
mock_connection.return_value = mock_conn |
| 99 |
|
| 100 |
response = FakeResponse(status_code=409) |
| 101 |
status_ex = exceptions.HttpException(response=response) |
| 102 |
|
| 103 |
mock_conn.instance_ha.create_notification.side_effect = status_ex |
| 104 |
notifier = masakari.SendNotification() |
| 105 |
notifier.send_notification( |
| 106 |
self.api_retry_max, self.api_retry_interval, self.event) |
| 107 |
|
| 108 |
mock_conn.instance_ha.create_notification.assert_called_once_with( |
| 109 |
type=self.event['notification']['type'], |
| 110 |
hostname=self.event['notification']['hostname'], |
| 111 |
generated_time=self.event['notification']['generated_time'], |
| 112 |
payload=self.event['notification']['payload']) |
| 113 |
|
| 114 |
@mock.patch.object(eventlet.greenthread, 'sleep') |
| 115 |
@mock.patch.object(connection, 'Connection') |
| 116 |
@mock.patch.object(ks_loading, 'load_session_from_conf_options') |
| 117 |
@mock.patch.object(ks_loading, 'load_auth_from_conf_options') |
| 118 |
def test_send_notification_500_error( |
| 119 |
self, mock_auth, mock_session, mock_connection, mock_sleep): |
| 120 |
|
| 121 |
mock_conn = mock.Mock() |
| 122 |
mock_conn.instance_ha.return_value = mock.Mock() |
| 123 |
mock_conn.instance_ha.create_notification.return_value = mock.Mock() |
| 124 |
mock_connection.return_value = mock_conn |
| 125 |
|
| 126 |
response = FakeResponse(status_code=500) |
| 127 |
status_ex = exceptions.HttpException(response=response) |
| 128 |
|
| 129 |
mock_conn.instance_ha.create_notification.side_effect = status_ex |
| 130 |
mock_sleep.return_value = None |
| 131 |
|
| 132 |
notifier = masakari.SendNotification() |
| 133 |
notifier.send_notification( |
| 134 |
self.api_retry_max, self.api_retry_interval, self.event) |
| 135 |
|
| 136 |
mock_conn.instance_ha.create_notification.assert_called_with( |
| 137 |
type=self.event['notification']['type'], |
| 138 |
hostname=self.event['notification']['hostname'], |
| 139 |
generated_time=self.event['notification']['generated_time'], |
| 140 |
payload=self.event['notification']['payload']) |
| 141 |
self.assertEqual(self.api_retry_max + 1, |
| 142 |
mock_conn.instance_ha.create_notification.call_count) |