vitrage/vitrage_tempest_tests/tests/api/event/test_events.py

115 lines
4.3 KiB
Python

# Copyright 2017 Nokia
#
# 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 six
from datetime import datetime
from oslo_log import log as logging
from oslotest import base
from vitrage.common.constants import EntityCategory
from vitrage.common.constants import EventProperties as EventProps
from vitrage.common.constants import VertexProperties as VProps
from vitrage import keystone_client
from vitrage import service
from vitrage_tempest_tests.tests.utils import wait_for_answer
from vitrageclient import client as v_client
LOG = logging.getLogger(__name__)
class TestEvents(base.BaseTestCase):
"""Test class for Vitrage event API"""
# noinspection PyPep8Naming
@classmethod
def setUpClass(cls):
cls.conf = service.prepare_service([])
cls.vitrage_client = \
v_client.Client('1', session=keystone_client.get_session(cls.conf))
def test_send_doctor_event_without_resource_id(self):
"""Sending an event in Doctor format should result in an alarm"""
details = {
'hostname': 'host123',
'source': 'sample_monitor',
'cause': 'another alarm',
'severity': 'critical',
'status': 'down',
'monitor_id': 'sample monitor',
'monitor_event_id': '456',
}
self._test_send_doctor_event(details)
def test_send_doctor_event_without_resource_id_v2(self):
"""Sending an event in Doctor format should result in an alarm"""
details = {
'hostname': 'host457',
'source': 'sample_monitor',
'cause': 'another alarm',
'severity': 'critical',
'status': 'down',
'monitor_id': 'sample monitor',
'monitor_event_id': '103',
}
self._test_send_doctor_event(details)
def _test_send_doctor_event(self, details):
try:
# post an event to the message bus
event_time = datetime.now()
event_time_iso = event_time.isoformat()
event_type = 'compute.host.down'
self.vitrage_client.event.post(event_time_iso, event_type, details)
api_alarms = wait_for_answer(2, 0.5, self._check_alarms)
# expect to get a 'host down alarm', generated by Doctor datasource
self.assertIsNotNone(api_alarms, 'Expected host down alarm')
self.assertEqual(1, len(api_alarms), 'Expected host down alarm')
alarm = api_alarms[0]
event_time_tz = six.u(event_time.strftime('%Y-%m-%dT%H:%M:%SZ'))
self._check_alarm(alarm, event_time_tz, event_type, details)
event_time = datetime.now()
event_time_iso = event_time.isoformat()
details['status'] = 'up'
self.vitrage_client.event.post(event_time_iso, event_type, details)
api_alarms = wait_for_answer(2, 0.5, self._check_alarms)
self.assertIsNotNone(api_alarms, 'Expected host down alarm')
self.assertEqual(0, len(api_alarms), 'Expected host down alarm')
except Exception as e:
LOG.exception(e)
raise
finally:
LOG.warning('done')
def _check_alarms(self):
api_alarms = self.vitrage_client.alarm.list(vitrage_id='all',
all_tenants=True)
if api_alarms:
return True, api_alarms
return False, api_alarms
def _check_alarm(self, alarm, event_time, event_type, details):
self.assertEqual(EntityCategory.ALARM, alarm[VProps.VITRAGE_CATEGORY])
self.assertEqual(event_type, alarm[VProps.NAME])
self.assertEqual(event_time, alarm[EventProps.TIME])
self.assertEqual(details['status'], alarm['status'])
self.assertFalse(alarm[VProps.VITRAGE_IS_DELETED])
self.assertFalse(alarm[VProps.VITRAGE_IS_PLACEHOLDER])