Zabbix datasource unit tests

Change-Id: I73fcab787dfa98bbfaf56b8e14347ca58e3a70e5
This commit is contained in:
Alexey Weyl 2016-06-13 11:06:24 +03:00
parent a63fff4f1f
commit 5485292397
10 changed files with 1070 additions and 0 deletions

View File

@ -349,3 +349,40 @@ def simple_nagios_alarm_generators(host_num,
})
return tg.get_trace_generators(test_entity_spec_list)
def simple_zabbix_alarm_generators(host_num,
events_num=0,
snap_vals=None):
"""A function for returning Zabbix alarm event generators.
Returns generators for a given number of Zabbix alarms.
:param host_num: number of hosts
:param events_num: number of snapshot alarms per hosts
:param snap_vals: preset vals for ALL snapshot events
:return: generators for zone_num zones as specified
"""
hosts = ['host-{0}'.format(index) for index in range(host_num)]
test_entity_spec_list = []
if events_num:
test_entity_spec_list.append({
tg.DYNAMIC_INFO_FKEY: tg.DRIVER_ZABBIX_SNAPSHOT_D,
tg.STATIC_INFO_FKEY: None,
tg.EXTERNAL_INFO_KEY: snap_vals,
tg.MAPPING_KEY: hosts,
tg.NAME_KEY: 'Zabbix alarm generator (alarm on)',
tg.NUM_EVENTS: max(events_num - len(hosts), 0)
})
test_entity_spec_list.append({
tg.DYNAMIC_INFO_FKEY: tg.DRIVER_ZABBIX_SNAPSHOT_D,
tg.STATIC_INFO_FKEY: None,
tg.EXTERNAL_INFO_KEY: snap_vals,
tg.MAPPING_KEY: hosts,
tg.NAME_KEY: 'Zabbix alarm generator (alarm off)',
tg.NUM_EVENTS: len(hosts)
})
return tg.get_trace_generators(test_entity_spec_list)

View File

@ -49,6 +49,7 @@ DRIVER_INST_SNAPSHOT_S = 'driver_inst_snapshot_static.json'
DRIVER_INST_UPDATE_D = 'driver_inst_update_dynamic.json'
DRIVER_NAGIOS_SNAPSHOT_D = 'driver_nagios_snapshot_dynamic.json'
DRIVER_NAGIOS_SNAPSHOT_S = 'driver_nagios_snapshot_static.json'
DRIVER_ZABBIX_SNAPSHOT_D = 'driver_zabbix_snapshot_dynamic.json'
DRIVER_SWITCH_SNAPSHOT_D = 'driver_switch_snapshot_dynamic.json'
DRIVER_VOLUME_UPDATE_D = 'driver_volume_update_dynamic.json'
DRIVER_VOLUME_SNAPSHOT_D = 'driver_volume_snapshot_dynamic.json'
@ -106,6 +107,7 @@ class EventTraceGenerator(object):
DRIVER_VOLUME_UPDATE_D: _get_volume_update_driver_values,
DRIVER_SWITCH_SNAPSHOT_D: _get_switch_snapshot_driver_values,
DRIVER_NAGIOS_SNAPSHOT_D: _get_nagios_alarm_driver_values,
DRIVER_ZABBIX_SNAPSHOT_D: _get_zabbix_alarm_driver_values,
DRIVER_CONSISTENCY_UPDATE_D:
_get_consistency_update_driver_values,
@ -442,6 +444,21 @@ def _get_nagios_alarm_driver_values(spec):
return static_values
def _get_zabbix_alarm_driver_values(spec):
hosts = spec[MAPPING_KEY]
static_info_re = None
if spec[STATIC_INFO_FKEY] is not None:
static_info_re = utils.load_specs(spec[STATIC_INFO_FKEY])
static_values = []
for host_name in hosts:
host_info = {'resource_name': host_name}
static_values.append(combine_data(
static_info_re, host_info, spec.get(EXTERNAL_INFO_KEY, None)
))
return static_values
def _get_trans_host_snapshot_values(spec):
"""Generates the static driver values for each host.

View File

@ -0,0 +1,12 @@
{
"sync_type": "zabbix",
"resource_type": "nova\\.host",
"resource_name": "compute-[1-9]",
"description": "CPU utilization|Check_MK|Uptime",
"status": "0",
"lastchange": "2016-02-07 15:26:04",
"sample_date": "2015-12-01T12:46:41Z",
"value": "1",
"priority": "1|2|3|4|5",
"sync_mode": "snapshot|init_snapshot|update"
}

View File

@ -0,0 +1,8 @@
zabbix:
- zabbix_host: compute-1
type: nova.host
name: compute-1
- zabbix_host: compute-2
type: nova.host
name: host2

View File

@ -11,6 +11,7 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from vitrage.datasources.nagios.properties import NagiosProperties \
as NagiosProps
from vitrage.tests import base

View File

@ -0,0 +1,55 @@
# Copyright 2016 - 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.
from vitrage.datasources.zabbix.driver import ZabbixDriver
from vitrage.tests.mocks import mock_driver
class MockZabbixDriver(ZabbixDriver):
"""A zabbix driver for tests.
Instead of calling Zabbix URL to get the data, it returns the data it
is asked to
"""
@staticmethod
def get_event_types(conf):
return []
@staticmethod
def enrich_event(event, event_type):
pass
@staticmethod
def get_topic(conf):
return None
def __init__(self, conf):
super(MockZabbixDriver, self).__init__(conf)
self.service_datas = None
def set_service_datas(self, service_datas):
self.service_datas = service_datas
def _get_alarms(self):
alarms = []
for service_data in self.service_datas:
generators = mock_driver.simple_zabbix_alarm_generators(
host_num=1,
events_num=1,
snap_vals=service_data)
alarms.append(
mock_driver.generate_sequential_events_list(generators)[0])
return alarms

View File

@ -0,0 +1,102 @@
# Copyright 2016 - 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.
from oslo_config import cfg
from oslo_log import log as logging
from vitrage.datasources.nova.host import NOVA_HOST_DATASOURCE
from vitrage.datasources.zabbix.driver import ZabbixDriver
from vitrage.datasources.zabbix.properties import ZabbixProperties \
as ZabbixProps
from vitrage.tests import base
from vitrage.tests.mocks import utils
LOG = logging.getLogger(__name__)
class TestZabbixConfig(base.BaseTest):
OPTS = [
cfg.StrOpt('transformer',
default='vitrage.datasources.zabbix.transformer.'
'ZabbixTransformer',
help='Zabbix data source transformer class path',
required=True),
cfg.StrOpt('driver',
default='vitrage.datasources.zabbix.driver.ZabbixDriver',
help='Zabbix driver class path',
required=True),
cfg.IntOpt('changes_interval',
default=30,
min=30,
help='interval between checking changes in zabbix plugin',
required=True),
cfg.StrOpt('user', default='admin',
help='Zabbix user name'),
cfg.StrOpt('password', default='zabbix',
help='Zabbix user password'),
cfg.StrOpt('url', default='', help='Zabbix url'),
cfg.StrOpt('config_file',
help='Zabbix configuration file',
default=utils.get_resources_dir()
+ '/zabbix/zabbix_conf.yaml'),
]
# the mappings match the ones in zabbix_conf.yaml
MAPPINGS = {
'compute-1': {ZabbixProps.RESOURCE_TYPE: NOVA_HOST_DATASOURCE,
ZabbixProps.RESOURCE_NAME: 'compute-1'},
'compute-2': {ZabbixProps.RESOURCE_TYPE: NOVA_HOST_DATASOURCE,
ZabbixProps.RESOURCE_NAME: 'host2'},
}
NON_EXISTING_MAPPINGS = {
'X': {ZabbixProps.RESOURCE_TYPE: NOVA_HOST_DATASOURCE,
ZabbixProps.RESOURCE_NAME: 'compute-1'},
'compute-1': {ZabbixProps.RESOURCE_TYPE: 'X',
ZabbixProps.RESOURCE_NAME: 'compute-1'},
}
# noinspection PyPep8Naming
@classmethod
def setUpClass(cls):
cls.conf = cfg.ConfigOpts()
cls.conf.register_opts(cls.OPTS, group='zabbix')
def test_zabbix_configuration_loading(self):
# Action
mappings = ZabbixDriver._configuration_mapping(self.conf)
# Test assertions
self.assertEqual(len(self.MAPPINGS), len(mappings))
for expected_mapping in self.MAPPINGS.items():
self.assertTrue(self._check_contains(expected_mapping, mappings))
for expected_mapping in self.NON_EXISTING_MAPPINGS.items():
self.assertFalse(self._check_contains(expected_mapping, mappings))
def test_zabbix_status_mapping(self):
# Action
mappings = ZabbixDriver._status_mapping()
# Test assertions
self.assertEqual(7, len(mappings))
@staticmethod
def _check_contains(expected_mapping, mappings):
for mapping in mappings.items():
if expected_mapping == mapping:
return True
return False

View File

@ -0,0 +1,647 @@
# Copyright 2016 - 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.
from oslo_config import cfg
from oslo_log import log as logging
from vitrage.common.constants import DatasourceProperties as DSProps
from vitrage.common.constants import EventAction
from vitrage.datasources.zabbix.properties import ZabbixProperties as \
ZabbixProps
from vitrage.tests.mocks import utils
from vitrage.tests.unit.datasources.zabbix.mock_driver import MockZabbixDriver
from vitrage.tests.unit.datasources.zabbix.zabbix_base_test import \
ZabbixBaseTest
LOG = logging.getLogger(__name__)
# noinspection PyProtectedMember
class ZabbixDriverTest(ZabbixBaseTest):
OPTS = [
cfg.StrOpt('config_file',
help='Zabbix configuration file',
default=utils.get_resources_dir()
+ '/zabbix/zabbix_conf.yaml'),
]
# noinspection PyPep8Naming
@classmethod
def setUpClass(cls):
cls.conf = cfg.ConfigOpts()
cls.conf.register_opts(cls.OPTS, group='zabbix')
def test_severity_retrieval(self):
# Setup
zabbix_driver = MockZabbixDriver(self.conf)
service_data1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization 1',
ZabbixProps.IS_ALARM_DISABLED: '1',
ZabbixProps.IS_ALARM_ON: '0',
ZabbixProps.SEVERITY: '1'}
service_data2 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization 2',
ZabbixProps.IS_ALARM_DISABLED: '1',
ZabbixProps.IS_ALARM_ON: '1',
ZabbixProps.SEVERITY: '1'}
service_data3 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization 3',
ZabbixProps.IS_ALARM_DISABLED: '0',
ZabbixProps.IS_ALARM_ON: '1',
ZabbixProps.SEVERITY: '1'}
service_data4 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization 4',
ZabbixProps.IS_ALARM_DISABLED: '0',
ZabbixProps.IS_ALARM_ON: '0',
ZabbixProps.SEVERITY: '1'}
zabbix_driver.set_service_datas([service_data1,
service_data2,
service_data3,
service_data4])
expected_service1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization 3',
ZabbixProps.SEVERITY: '1'}
# Action
services = zabbix_driver._get_all_alarms()
# Test assertions
# Services with status OK should not be returned
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(1, len(services))
self._assert_contains(expected_service1, services)
def test_get_all(self):
"""Check get_all functionality.
Checks which tests are returned when performing get_all action:
tests that their status is not OK, or tests that their status changed
from not-OK to OK
"""
# Setup
zabbix_driver = MockZabbixDriver(self.conf)
# Action
service_data1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '-1'}
service_data2 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '-1'}
service_data3 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'Uptime',
ZabbixProps.SEVERITY: '-1'}
zabbix_driver.set_service_datas([service_data1,
service_data2,
service_data3])
services = zabbix_driver._get_all_alarms()
# Test assertions
# Services with status OK should not be returned
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(0, len(services))
# Action
service_data1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '1'}
service_data2 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '-1'}
service_data3 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'Uptime',
ZabbixProps.SEVERITY: '-1'}
zabbix_driver.set_service_datas([service_data1,
service_data2,
service_data3])
services = zabbix_driver._get_all_alarms()
# Test assertions
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(1, len(services))
self._assert_contains(service_data1, services)
# Action
service_data1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '4'}
service_data2 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '1'}
service_data3 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'Uptime',
ZabbixProps.SEVERITY: '-1'}
zabbix_driver.set_service_datas([service_data1,
service_data2,
service_data3])
excpected_service1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '4'}
excpected_service2 = {ZabbixProps.RESOURCE_NAME: 'host2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '1'}
services = zabbix_driver._get_all_alarms()
# Test assertions
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(2, len(services))
self._assert_contains(excpected_service1, services)
self._assert_contains(excpected_service2, services)
# Action
service_data1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '-1'}
service_data2 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '-1'}
service_data3 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'Uptime',
ZabbixProps.SEVERITY: '-1'}
zabbix_driver.set_service_datas([service_data1,
service_data2,
service_data3])
excpected_service1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '-1'}
excpected_service2 = {ZabbixProps.RESOURCE_NAME: 'host2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '-1'}
services = zabbix_driver._get_all_alarms()
# Test assertions
# The services of service_data1/2 should be returned although their
# status is OK, because they were not OK earlier
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(2, len(services))
self._assert_contains(excpected_service1, services)
self._assert_contains(excpected_service2, services)
# Action
services = zabbix_driver._get_all_alarms()
# Test assertions
# Calling get_services again should not return anything, since all
# services are still OK
self.assertIsNotNone(services, 'services is None')
self.assertEqual(0, len(services))
def test_get_changes(self):
"""Check get_changes functionality.
Checks which tests are returned when performing get_changes action:
Tests that their status was changed since the last call
"""
# Setup
zabbix_driver = MockZabbixDriver(self.conf)
# Action
service_data1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '-1'}
service_data2 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '-1'}
service_data3 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'Uptime',
ZabbixProps.SEVERITY: '-1'}
zabbix_driver.set_service_datas([service_data1,
service_data2,
service_data3])
services = zabbix_driver._get_changed_alarms()
# Test assertions
# Services with status OK should not be returned
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(0, len(services))
# Action
service_data1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '1'}
service_data2 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '-1'}
service_data3 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'Uptime',
ZabbixProps.SEVERITY: '-1'}
zabbix_driver.set_service_datas([service_data1,
service_data2,
service_data3])
services = zabbix_driver._get_changed_alarms()
# Test assertions
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(1, len(services))
self._assert_contains(service_data1, services)
# Action
service_data1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '4'}
service_data2 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '1'}
service_data3 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'Uptime',
ZabbixProps.SEVERITY: '-1'}
zabbix_driver.set_service_datas([service_data1,
service_data2,
service_data3])
excpected_service1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '4'}
excpected_service2 = {ZabbixProps.RESOURCE_NAME: 'host2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '1'}
services = zabbix_driver._get_changed_alarms()
# Test assertions
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(2, len(services))
self._assert_contains(excpected_service1, services)
self._assert_contains(excpected_service2, services)
# Action
service_data1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '4'}
service_data2 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '4'}
service_data3 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'Uptime',
ZabbixProps.SEVERITY: '-1'}
zabbix_driver.set_service_datas([service_data1,
service_data2,
service_data3])
excpected_service1 = {ZabbixProps.RESOURCE_NAME: 'host2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '4'}
services = zabbix_driver._get_changed_alarms()
# Test assertions
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(1, len(services))
self._assert_contains(excpected_service1, services)
# Action
service_data1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '-1'}
service_data2 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '-1'}
service_data3 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'Uptime',
ZabbixProps.SEVERITY: '-1'}
zabbix_driver.set_service_datas([service_data1,
service_data2,
service_data3])
excpected_service1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '-1'}
excpected_service2 = {ZabbixProps.RESOURCE_NAME: 'host2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '-1'}
services = zabbix_driver._get_changed_alarms()
# Test assertions
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(2, len(services))
self._assert_contains(excpected_service1, services)
self._assert_contains(excpected_service2, services)
# Action
services = zabbix_driver._get_changed_alarms()
# Test assertions
self.assertIsNotNone(services, 'services is None')
self.assertEqual(0, len(services))
def test_get_changes_and_get_all(self):
"""Check get_changes and get_all functionalities """
# Setup
zabbix_driver = MockZabbixDriver(self.conf)
# Action
service_data1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '1'}
service_data2 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '-1'}
service_data3 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'Uptime',
ZabbixProps.SEVERITY: '-1'}
zabbix_driver.set_service_datas([service_data1,
service_data2,
service_data3])
services = zabbix_driver._get_changed_alarms()
# Test assertions
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(1, len(services))
self._assert_contains(service_data1, services)
# Action
services = zabbix_driver._get_changed_alarms()
# Test assertions
# Calling get_changes for the second time should return nothing
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(0, len(services))
# Action
services = zabbix_driver._get_all_alarms()
# Test assertions
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(1, len(services))
self._assert_contains(service_data1, services)
# Action
services = zabbix_driver._get_all_alarms()
# Test assertions
# Calling get_all for the second time should return the same results
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(1, len(services))
self._assert_contains(service_data1, services)
# Action
service_data1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '4'}
service_data2 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '1'}
service_data3 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'Uptime',
ZabbixProps.SEVERITY: '-1'}
zabbix_driver.set_service_datas([service_data1,
service_data2,
service_data3])
excpected_service1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '4'}
excpected_service2 = {ZabbixProps.RESOURCE_NAME: 'host2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '1'}
services = zabbix_driver._get_all_alarms()
# Test assertions
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(2, len(services))
self._assert_contains(excpected_service1, services)
self._assert_contains(excpected_service2, services)
# Action
services = zabbix_driver._get_changed_alarms()
# Test assertions
# Calling get_changes after get_all should return nothing
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(0, len(services))
# Action
services = zabbix_driver._get_all_alarms()
# Test assertions
# Calling get_all for the second time should return the same results
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(2, len(services))
self._assert_contains(excpected_service1, services)
self._assert_contains(excpected_service2, services)
# Action
service_data1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '4'}
service_data2 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '4'}
service_data3 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'Uptime',
ZabbixProps.SEVERITY: '4'}
zabbix_driver.set_service_datas([service_data1,
service_data2,
service_data3])
excpected_service1 = {ZabbixProps.RESOURCE_NAME: 'host2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '4'}
excpected_service2 = {ZabbixProps.RESOURCE_NAME: 'host2',
ZabbixProps.DESCRIPTION: 'Uptime',
ZabbixProps.SEVERITY: '4'}
services = zabbix_driver._get_changed_alarms()
# Test assertions
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(2, len(services))
self._assert_contains(excpected_service1, services)
self._assert_contains(excpected_service2, services)
# Action
service_data1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '1'}
service_data2 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '4'}
service_data3 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'Uptime',
ZabbixProps.SEVERITY: '4'}
zabbix_driver.set_service_datas([service_data1,
service_data2,
service_data3])
expected_service1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '1'}
services = zabbix_driver._get_changed_alarms()
# Test assertions
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(1, len(services))
self._assert_contains(expected_service1, services)
excpected_service1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '1'}
excpected_service2 = {ZabbixProps.RESOURCE_NAME: 'host2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '4'}
excpected_service3 = {ZabbixProps.RESOURCE_NAME: 'host2',
ZabbixProps.DESCRIPTION: 'Uptime',
ZabbixProps.SEVERITY: '4'}
# Action
services = zabbix_driver._get_changed_alarms()
# Test assertions
self.assertIsNotNone(services, 'services is None')
self.assertEqual(0, len(services))
# Action
services = zabbix_driver._get_all_alarms()
# Test assertions
# Calling get_all for the second time should return the same results
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(3, len(services))
self._assert_contains(excpected_service1, services)
self._assert_contains(excpected_service2, services)
self._assert_contains(excpected_service3, services)
def test_delete_service(self):
"""Check get_all and get_changes with a deleted service"""
# Setup
zabbix_driver = MockZabbixDriver(self.conf)
# Action
service_data1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '1'}
service_data2 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '-1'}
service_data3 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'Uptime',
ZabbixProps.SEVERITY: '-1'}
zabbix_driver.set_service_datas([service_data1,
service_data2,
service_data3])
services = zabbix_driver._get_all_alarms()
# Test assertions
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(1, len(services))
self._assert_contains(service_data1, services)
# Action - delete a service that was OK
service_data1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '1'}
service_data2 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '-1'}
zabbix_driver.set_service_datas([service_data1, service_data2])
services = zabbix_driver._get_all_alarms()
# Test assertions
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(1, len(services))
self._assert_contains(service_data1, services)
# Action - delete a service that was not OK
service_data2 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '-1'}
zabbix_driver.set_service_datas([service_data2])
services = zabbix_driver._get_all_alarms()
# Test assertions
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(1, len(services))
self._assert_contains(service_data1, services)
self.assertEqual(EventAction.DELETE_ENTITY,
services[0][DSProps.EVENT_TYPE])
# Action - get changes, should not return the deleted alarm again
services = zabbix_driver._get_changed_alarms()
# Test assertions
self.assertIsNotNone(services, 'services is None')
self.assertEqual(0, len(services))
# Action - "undelete" the service that was OK
service_data1 = {ZabbixProps.RESOURCE_NAME: 'compute-1',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '1'}
service_data2 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '-1'}
zabbix_driver.set_service_datas([service_data1, service_data2])
services = zabbix_driver._get_all_alarms()
# Test assertions
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(1, len(services))
self._assert_contains(service_data1, services)
self.assertFalse(DSProps.EVENT_TYPE in services[0])
# Action - delete a service that was not OK and call get_changes
service_data2 = {ZabbixProps.RESOURCE_NAME: 'compute-2',
ZabbixProps.DESCRIPTION: 'CPU utilization',
ZabbixProps.SEVERITY: '-1'}
zabbix_driver.set_service_datas([service_data2])
services = zabbix_driver._get_changed_alarms()
# Test assertions
self.assertIsNotNone(services, 'No services returned')
self.assertEqual(1, len(services))
self._assert_contains(service_data1, services)
self.assertEqual(EventAction.DELETE_ENTITY,
services[0][DSProps.EVENT_TYPE])

View File

@ -0,0 +1,155 @@
# Copyright 2016 - 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.
from oslo_log import log as logging
from vitrage.common.constants import DatasourceProperties as DSProps
from vitrage.common.constants import EdgeLabel
from vitrage.common.constants import EntityCategory
from vitrage.common.constants import EventAction
from vitrage.common.constants import SyncMode
from vitrage.common.constants import VertexProperties as VProps
from vitrage.datasources.alarm_properties import AlarmProperties as AlarmProps
from vitrage.datasources.nova.host import NOVA_HOST_DATASOURCE
from vitrage.datasources.nova.host.transformer import HostTransformer
from vitrage.datasources.transformer_base import TransformerBase
from vitrage.datasources.zabbix.driver import ZabbixDriver
from vitrage.datasources.zabbix.properties import ZabbixProperties
from vitrage.datasources.zabbix.properties import ZabbixTriggerStatus
from vitrage.datasources.zabbix.transformer import ZabbixTransformer
from vitrage.tests import base
from vitrage.tests.mocks import mock_driver as mock_sync
LOG = logging.getLogger(__name__)
# noinspection PyProtectedMember
class ZabbixTransformerTest(base.BaseTest):
# noinspection PyAttributeOutsideInit,PyPep8Naming
@classmethod
def setUpClass(cls):
cls.transformers = {}
host_transformer = HostTransformer(cls.transformers)
cls.transformers[NOVA_HOST_DATASOURCE] = host_transformer
def test_extract_key(self):
LOG.debug('Test get key from nova instance transformer')
# Test setup
spec_list = mock_sync.simple_zabbix_alarm_generators(host_num=1,
events_num=1)
zabbix_alarms = mock_sync.generate_sequential_events_list(spec_list)
transformer = ZabbixTransformer(self.transformers)
event = zabbix_alarms[0]
# Test action
observed_key = transformer._create_entity_key(event)
# Test assertions
observed_key_fields = observed_key.split(
TransformerBase.KEY_SEPARATOR)
self.assertEqual(EntityCategory.ALARM, observed_key_fields[0])
self.assertEqual(event[DSProps.SYNC_TYPE], observed_key_fields[1])
self.assertEqual(event[ZabbixProperties.RESOURCE_NAME],
observed_key_fields[2])
self.assertEqual(event[ZabbixProperties.DESCRIPTION],
observed_key_fields[3])
def test_zabbix_alarm_transform(self):
LOG.debug('Zabbix alarm transformer test: transform entity event')
# Test setup
spec_list = mock_sync.simple_zabbix_alarm_generators(host_num=4,
events_num=10)
zabbix_alarms = mock_sync.generate_sequential_events_list(spec_list)
# convert to correct status
for alar in zabbix_alarms:
alar[ZabbixProperties.STATUS] = \
ZabbixDriver._status_mapping()[alar[ZabbixProperties.SEVERITY]]
for alarm in zabbix_alarms:
# Test action
wrapper = ZabbixTransformer(self.transformers).transform(alarm)
self._validate_vertex(wrapper.vertex, alarm)
neighbors = wrapper.neighbors
self.assertEqual(1, len(neighbors))
neighbor = neighbors[0]
# Right now we are support only host as a resource
if neighbor.vertex[VProps.TYPE] == NOVA_HOST_DATASOURCE:
self._validate_host_neighbor(neighbors[0], alarm)
self._validate_action(alarm, wrapper)
def _validate_action(self, alarm, wrapper):
sync_mode = alarm[DSProps.SYNC_MODE]
if sync_mode in (SyncMode.SNAPSHOT, SyncMode.UPDATE):
if alarm[ZabbixProperties.STATUS] == 'OK':
self.assertEqual(EventAction.DELETE_ENTITY, wrapper.action)
else:
self.assertEqual(EventAction.UPDATE_ENTITY, wrapper.action)
else:
self.assertEqual(EventAction.CREATE_ENTITY, wrapper.action)
def _validate_vertex(self, vertex, event):
self.assertEqual(EntityCategory.ALARM, vertex[VProps.CATEGORY])
self.assertEqual(event[DSProps.SYNC_TYPE], vertex[VProps.TYPE])
self.assertEqual(event[ZabbixProperties.DESCRIPTION],
vertex[VProps.NAME])
event_status = event[ZabbixProperties.STATUS]
if event_status == ZabbixTriggerStatus.OK:
self.assertEqual(AlarmProps.INACTIVE_STATE,
vertex[VProps.STATE])
else:
self.assertEqual(AlarmProps.ACTIVE_STATE,
vertex[VProps.STATE])
self.assertEqual(event_status, vertex[VProps.SEVERITY])
self.assertFalse(vertex[VProps.IS_DELETED])
self.assertFalse(vertex[VProps.IS_PLACEHOLDER])
def _validate_host_neighbor(self, neighbor, event):
host_vertex = neighbor.vertex
key_fields = host_vertex.vertex_id.split(TransformerBase.KEY_SEPARATOR)
self.assertEqual(EntityCategory.RESOURCE, key_fields[0])
self.assertEqual(NOVA_HOST_DATASOURCE, key_fields[1])
self.assertEqual(event[ZabbixProperties.RESOURCE_NAME], key_fields[2])
self.assertFalse(host_vertex[VProps.IS_DELETED])
self.assertTrue(host_vertex[VProps.IS_PLACEHOLDER])
self.assertEqual(EntityCategory.RESOURCE, host_vertex[VProps.CATEGORY])
self.assertEqual(event[ZabbixProperties.RESOURCE_NAME],
host_vertex[VProps.ID])
self.assertEqual(NOVA_HOST_DATASOURCE, host_vertex[VProps.TYPE])
edge = neighbor.edge
self.assertEqual(EdgeLabel.ON, edge.label)
alarm_key = \
ZabbixTransformer(self.transformers)._create_entity_key(event)
self.assertEqual(alarm_key, edge.source_id)
self.assertEqual(host_vertex.vertex_id, edge.target_id)

View File

@ -0,0 +1,36 @@
# Copyright 2016 - 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.
from vitrage.datasources.zabbix.properties import ZabbixProperties \
as ZabbixProps
from vitrage.tests import base
class ZabbixBaseTest(base.BaseTest):
def _assert_contains(self, expected_serv, services):
for service in services:
if service[ZabbixProps.RESOURCE_NAME] == \
expected_serv[ZabbixProps.RESOURCE_NAME] and \
service[ZabbixProps.DESCRIPTION] == \
expected_serv[ZabbixProps.DESCRIPTION]:
self._assert_expected_service(expected_serv, service)
return
self.fail("service not found: %(resource_name)s %(service_name)s" %
{'resource_name': expected_serv[ZabbixProps.RESOURCE_NAME],
'service_name': expected_serv[ZabbixProps.SERVICE]})
def _assert_expected_service(self, expected_service, service):
for key, value in expected_service.items():
self.assertEqual(value, service[key], 'wrong value for ' + key)