Dan Offek 589ca87ef7 Fix Doctor test
Change-Id: Ia897628ccb86cb8099434ad6c935b94e7b118af9
2017-04-20 15:19:17 +00:00

116 lines
4.2 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
import time
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 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(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 = self._wait_for_status(2, 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)
self._wait_for_status(2, self._check_alarms)
# TODO(iaffek)
# 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.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.IS_DELETED])
self.assertFalse(alarm[VProps.IS_PLACEHOLDER])
@staticmethod
def _wait_for_status(max_waiting, func, **kwargs):
count = 0
status, res = False, None
while count < max_waiting:
status, res = func(**kwargs)
if status:
return res
count += 1
time.sleep(2)
LOG.info("wait_for_status - False")
return res