def test_actions_buttons_are_updated(self): """Actions Buttons are Updated""" under_test = ActionsQWidget() self.assertIsNone(under_test.item) host_test = Host() host_test.create( 'id_1', { 'ls_acknowledged': False, 'ls_downtimed': False, '_id': 'id_1', 'ls_state': 'DOWN' }, 'name' ) under_test.initialize(host_test) # Actions buttons are True by default self.assertTrue(under_test.acknowledge_btn.isEnabled()) self.assertTrue(under_test.downtime_btn.isEnabled()) self.assertIsNotNone(under_test.item) # Log to backend and fill datamanager app_backend.login( settings.get_config('Alignak', 'username'), settings.get_config('Alignak', 'password') ) data_manager.update_database('host', [host_test]) # Update widget under_test.update_widget() webservice_test = settings.get_config('Alignak', 'webservice') self.assertTrue(webservice_test) if app_backend.ws_client.auth: # WS is set but not available, so buttons are disabled self.assertTrue(under_test.acknowledge_btn.isEnabled()) self.assertTrue(under_test.downtime_btn.isEnabled()) else: # WS is set but not available, so buttons are disabled self.assertFalse(under_test.acknowledge_btn.isEnabled()) self.assertFalse(under_test.downtime_btn.isEnabled()) # remove WS settings.set_config('Alignak', 'webservice', '') under_test.update_widget() # Only downtime button is Enabled, because host is UP self.assertTrue(under_test.acknowledge_btn.isEnabled()) self.assertTrue(under_test.downtime_btn.isEnabled()) # Restore configurations settings.set_config('Alignak', 'webservice', webservice_test) app_backend.connected = False app_backend.user = {}
def test_actions_buttons_are_updated(self): """Actions Buttons are Updated""" under_test = ActionsQWidget() self.assertIsNone(under_test.item) host_test = Host() host_test.create( 'id_1', { 'ls_acknowledged': False, 'ls_downtimed': False, '_id': 'id_1', 'ls_state': 'DOWN' }, 'name') under_test.initialize(host_test) # Actions buttons are True by default self.assertTrue(under_test.acknowledge_btn.isEnabled()) self.assertTrue(under_test.downtime_btn.isEnabled()) self.assertIsNotNone(under_test.item) # Log to backend and fill datamanager app_backend.login(settings.get_config('Alignak', 'username'), settings.get_config('Alignak', 'password')) data_manager.update_database('host', [host_test]) # Update widget under_test.update_widget() webservice_test = settings.get_config('Alignak', 'webservice') self.assertTrue(webservice_test) if app_backend.ws_client.auth: # WS is set but not available, so buttons are disabled self.assertTrue(under_test.acknowledge_btn.isEnabled()) self.assertTrue(under_test.downtime_btn.isEnabled()) else: # WS is set but not available, so buttons are disabled self.assertFalse(under_test.acknowledge_btn.isEnabled()) self.assertFalse(under_test.downtime_btn.isEnabled()) # remove WS settings.set_config('Alignak', 'webservice', '') under_test.update_widget() # Only downtime button is Enabled, because host is UP self.assertTrue(under_test.acknowledge_btn.isEnabled()) self.assertTrue(under_test.downtime_btn.isEnabled()) # Restore configurations settings.set_config('Alignak', 'webservice', webservice_test) app_backend.connected = False app_backend.user = {}
def get_host_resume_widget(self): """ Return Host resume QWidget """ self.layout.addWidget(QLabel(_('<b>Hosts:</b>')), 0, 0, 1, 1) self.items_nb['hosts_nb'].setObjectName('subtitle') self.layout.addWidget(self.items_nb['hosts_nb'], 1, 0, 1, 1) row = 1 for icon in Host.get_available_icons(): self.hosts_buttons[icon].setIcon(QIcon(settings.get_image(icon))) self.hosts_buttons[icon].setFixedSize(48, 24) self.hosts_buttons[icon].setObjectName(icon) self.hosts_buttons[icon].setToolTip( _('Hosts %s. See in WebUI ?') % icon.replace('hosts_', '').upper() ) self.hosts_buttons[icon].clicked.connect( lambda: self.open_item_type_url('hosts') ) self.layout.addWidget(self.hosts_buttons[icon], 0, row, 1, 1) self.layout.setAlignment(self.hosts_buttons[icon], Qt.AlignCenter) self.hosts_labels[icon].setObjectName(icon) self.layout.addWidget(self.hosts_labels[icon], 1, row, 1, 1) self.layout.setAlignment(self.hosts_labels[icon], Qt.AlignCenter) row += 1
def test_remove_event(self): """Remove Spy Event Item""" under_test = SpyQWidget() under_test.initialize() spy_item_test = EventItem() spy_item_test.initialize('OK', 'Message', host='_id_1') # Fill database host_test = Host() host_test.create( '_id_1', { '_id': '_id_1', 'ls_downtimed': False, 'ls_acknowledged': False, 'active_checks_enabled': True, 'passive_checks_enabled': True, '_overall_state_id': 4, 'ls_state': 'DOWN' }, 'hostname') data_manager.update_database('host', [host_test]) # No item spy, so item is None self.assertIsNone(under_test.spy_list_widget.item(1)) self.assertFalse(under_test.spy_list_widget.spied_hosts) # Spy host under_test.spy_list_widget.add_spy_host(host_test.item_id) # Assert EventItem have same host "_id" that the one added self.assertEqual(spy_item_test.host, under_test.spy_list_widget.item(0).host) self.assertEqual(['_id_1'], under_test.spy_list_widget.spied_hosts) # Set this item to current one and add it to spy list under_test.spy_list_widget.setCurrentRow(0) under_test.spy_list_widget.setCurrentItem( under_test.spy_list_widget.item(0)) under_test.remove_event() # Event is no more here self.assertNotEqual(spy_item_test.host, under_test.spy_list_widget.item(0).host) self.assertFalse(under_test.spy_list_widget.spied_hosts)
def test_get_request_host_model(self): """Get Host Request Model""" under_test = Host.get_request_model() self.assertTrue('endpoint' in under_test) self.assertEqual('host', under_test['endpoint']) self.assertTrue('params' in under_test) self.assertTrue('projection' in under_test)
def test_get_overall_tooltip(self): """Get Overall Tooltip of Host""" under_test = Host() under_test.create( '_id1', { '_overall_state_id': 1, 'ls_state': 'DOWN', 'ls_downtimed': True, 'ls_acknowledged': False, 'active_checks_enabled': True, 'passive_checks_enabled': False, }, 'hostname') tooltip_test = under_test.get_overall_tooltip([]) self.assertEqual('Hostname is DOWN but downtimed, no services...', tooltip_test)
def test_get_overall_tooltip(self): """Get Overall Tooltip of Host""" under_test = Host() under_test.create( '_id1', { '_overall_state_id': 1, 'ls_state': 'DOWN', 'ls_downtimed': True, 'ls_acknowledged': False, 'active_checks_enabled': True, 'passive_checks_enabled': False, }, 'hostname' ) tooltip_test = under_test.get_overall_tooltip([]) self.assertEqual('Hostname is DOWN but downtimed, no services...', tooltip_test)
def query_hosts(self): """ Launch request on "host" endpoint, add hosts in problems if needed """ request_model = Host.get_request_model() request = self.get( request_model['endpoint'], request_model['params'], request_model['projection'], all_items=True ) if request: hosts_list = [] for backend_item in request['_items']: host = Host() host.create( backend_item['_id'], backend_item, backend_item['name'], ) hosts_list.append(host) # If host is a problem, add / update it if data_manager.is_problem('host', backend_item): if data_manager.get_item('problems', host.item_id): data_manager.update_item_data('problems', host.item_id, host.data) else: data_manager.database['problems'].append(host) data_manager.db_is_ready['problems']['host'] = True if hosts_list: data_manager.update_database('host', hosts_list) if 'OK' in request['_status']: data_manager.db_is_ready[request_model['endpoint']] = True
def test_initialize_actions_widget(self): """Initialize Actions QWidget""" under_test = ActionsQWidget() self.assertIsNotNone(under_test.acknowledge_btn) self.assertIsNotNone(under_test.downtime_btn) self.assertIsNone(under_test.item) host_test = Host() under_test.initialize(host_test) self.assertIsNotNone(under_test.item)
def query_hosts(self): """ Launch request on "host" endpoint, add hosts in problems if needed """ request_model = Host.get_request_model() request = self.get(request_model['endpoint'], request_model['params'], request_model['projection'], all_items=True) if request: hosts_list = [] for backend_item in request['_items']: host = Host() host.create( backend_item['_id'], backend_item, backend_item['name'], ) hosts_list.append(host) # If host is a problem, add / update it if data_manager.is_problem('host', backend_item): if data_manager.get_item('problems', host.item_id): data_manager.update_item_data('problems', host.item_id, host.data) else: data_manager.database['problems'].append(host) data_manager.db_is_ready['problems']['host'] = True if hosts_list: data_manager.update_database('host', hosts_list) if 'OK' in request['_status']: data_manager.db_is_ready[request_model['endpoint']] = True
def test_manage_host_events_with_wrong_row(self): """Manage Host Events With Wrong Row""" under_test = SpyQWidget() under_test.initialize() # Hint item in spy list widget, 0 item in host list widget self.assertEqual(1, under_test.spy_list_widget.count()) self.assertEqual(0, under_test.host_list_widget.count()) # Filling database host_test = Host() host_test.create( 'spy1', { '_id': 'spy1', 'ls_downtimed': False, 'ls_acknowledged': False, 'active_checks_enabled': True, 'passive_checks_enabled': True, '_overall_state_id': 4, 'ls_state': 'DOWN' }, 'hostname') data_manager.update_database('host', [host_test]) # Spy this host under_test.spy_list_widget.add_spy_host(host_test.item_id) # Host have been added in list widget, 0 item in host list widget self.assertEqual(1, under_test.spy_list_widget.count()) self.assertEqual(host_test.item_id, under_test.spy_list_widget.item(0).host) self.assertEqual(0, under_test.host_list_widget.count()) # Manage problems under_test.manage_host_events(-1) # Host list equal to 0 with row equal to -1 self.assertEqual(0, under_test.host_list_widget.count())
def test_send_spy_events(self): """Send Spy Events""" init_event_widget() under_test = SpyQWidget() under_test.initialize() # Hint item is here self.assertEqual(1, under_test.spy_list_widget.count()) # Filling database host_test = Host() host_test.create( 'spy1', { '_id': 'spy1', 'ls_downtimed': False, 'ls_acknowledged': False, 'active_checks_enabled': True, 'passive_checks_enabled': True, '_overall_state_id': 4, 'ls_state': 'DOWN' }, 'hostname') host_test_2 = Host() host_test_2.create( 'spy2', { '_id': 'spy2', 'ls_downtimed': False, 'ls_acknowledged': False, 'active_checks_enabled': True, 'passive_checks_enabled': True, '_overall_state_id': 4, 'ls_state': 'DOWN' }, 'hostname') data_manager.update_database('host', [host_test, host_test_2]) under_test.spy_list_widget.add_spy_host('spy1') under_test.spy_list_widget.add_spy_host('spy2') # Item hint have been removed, host spied added self.assertEqual(2, under_test.spy_list_widget.count()) under_test.send_spy_events() # Sending events does not modify spy list widget count self.assertEqual(2, under_test.spy_list_widget.count())
class TestProblemsQWidget(unittest2.TestCase): """ This file test the ProblemsQWidget class. """ # Host data test host_list = [] for i in range(0, 10): host = Host() host.create( '_id%d' % i, { 'name': 'host%d' % i, 'alias': 'Host %d' % i, '_id': '_id%d' % i, 'ls_downtimed': False, 'ls_acknowledged': False, 'ls_state': 'DOWN', 'ls_output': 'output host %d' % i }, 'host%d' % i) host_list.append(host) # Service data test service_list = [] for i in range(0, 10): service = Service() service.create( '_id%d' % i, { 'name': 'service%d' % i, 'host': '_id%d' % i, '_id': '_id%d' % i, 'ls_state': 'CRITICAL', 'ls_acknowledged': False, 'ls_downtimed': False, 'ls_output': 'output service %d' % i }, 'service%d' % i) service_list.append(service) @classmethod def setUpClass(cls): """Create QApplication""" try: cls.app = QApplication(sys.argv) except: pass def test_initialize(self): """Initialize ProblemsQWidget""" under_test = ProblemsQWidget() self.assertIsNotNone(under_test.layout) self.assertTrue(under_test.problems_table) self.assertTrue(under_test.problems_title) self.assertFalse(under_test.filter_hosts_btn.is_checked()) self.assertFalse(under_test.filter_services_btn.is_checked()) under_test.initialize(None) self.assertIsNotNone(under_test.layout) self.assertTrue(under_test.problems_table) self.assertTrue(under_test.problems_title) self.assertFalse(under_test.filter_hosts_btn.is_checked()) self.assertFalse(under_test.filter_services_btn.is_checked()) self.assertEqual('title', under_test.problems_title.objectName()) def test_add_spy_host(self): """Add Spy Host from Problems QWidget""" data_manager.update_database('host', []) data_manager.update_database('service', []) under_test = ProblemsQWidget() spy_widget_test = SpyQWidget() spy_widget_test.initialize() under_test.initialize(spy_widget_test) # Update view with problems under_test.problems_table.update_view( {'problems': [self.host_list[8]]}) # Make this QStandardItem as current index index_test = under_test.problems_table.model().index(0, 0) under_test.problems_table.selectionModel().setCurrentIndex( index_test, QItemSelectionModel.SelectCurrent) self.assertFalse(under_test.spy_widget.spy_list_widget.spied_hosts) under_test.add_spied_host() # Assert host has been spied self.assertTrue(under_test.spy_widget.spy_list_widget.spied_hosts) # "_id8" is inside "spied_hosts" self.assertTrue(self.host_list[8].item_id in under_test.spy_widget.spy_list_widget.spied_hosts) def test_update_problems_data(self): """Update Problems Data""" # Reset Problems data_manager.database['problems'] = [] # Initialize QWidget under_test = ProblemsQWidget() spy_widget_test = SpyQWidget() spy_widget_test.initialize() under_test.initialize(spy_widget_test) model_test = under_test.problems_table.model() select_model_test = under_test.problems_table.selectionModel() under_test.update_problems_data() # Assert Table models have changed self.assertNotEqual(model_test, under_test.problems_table.model()) self.assertNotEqual(select_model_test, under_test.problems_table.selectionModel()) # Add problems for item in self.host_list: data_manager.database['problems'].append(item) for item in self.service_list: data_manager.database['problems'].append(item) # Assert filter buttons are False self.assertFalse(under_test.filter_hosts_btn.is_checked()) self.assertFalse(under_test.filter_services_btn.is_checked()) # Even if a filter is given, # the view does not filter by item type if filter buttons are False under_test.update_problems_data('service') # Collect items items_test = [ under_test.problems_table.model().data( under_test.problems_table.model().index(row, 0), Qt.UserRole) for row in range(under_test.problems_table.model().rowCount()) ] self.assertEqual(20, len(items_test)) def test_update_problems_data_keep_linesearch_text(self): """Update Problems Data Keep LineSearch Text""" under_test = ProblemsQWidget() spy_widget_test = SpyQWidget() spy_widget_test.initialize() under_test.initialize(None) self.assertFalse(under_test.line_search.text()) # Set text of QLineEdit under_test.line_search.setText('research') under_test.update_problems_data() # After update, text is keeped self.assertEqual('research', under_test.line_search.text()) def test_filter_hosts(self): """Filter Hosts in Problems View""" under_test = ProblemsQWidget() spy_widget_test = SpyQWidget() spy_widget_test.initialize() under_test.initialize(spy_widget_test) # Add problems data_manager.database['problems'] = [] for item in self.host_list: data_manager.database['problems'].append(item) for item in self.service_list: data_manager.database['problems'].append(item) # Update and filter Hosts under_test.filter_hosts_btn.update_btn_state(True) under_test.update_problems_data('host') # Assert service filter button is False self.assertFalse(under_test.filter_services_btn.is_checked()) # Collect items items_test = [ under_test.problems_table.model().data( under_test.problems_table.model().index(row, 0), Qt.UserRole) for row in range(under_test.problems_table.model().rowCount()) ] self.assertEqual(10, len(items_test)) for item in items_test: self.assertIsInstance(item, Host) def test_filter_services(self): """Filter Services in Problems View""" under_test = ProblemsQWidget() spy_widget_test = SpyQWidget() spy_widget_test.initialize() under_test.initialize(spy_widget_test) # Update and filter Services under_test.filter_services_btn.update_btn_state(True) under_test.update_problems_data('service') # Assert host filter button is False self.assertFalse(under_test.filter_hosts_btn.is_checked()) # Collect items items_test = [ under_test.problems_table.model().data( under_test.problems_table.model().index(row, 0), Qt.UserRole) for row in range(under_test.problems_table.model().rowCount()) ] self.assertEqual(10, len(items_test)) for item in items_test: self.assertIsInstance(item, Service)
def test_manage_host_events_with_valid_rows(self): """Manage Host Events With Valid Rows""" under_test = SpyQWidget() under_test.initialize() # Filling "host" database host_test = Host() host_test.create( 'spy1', { '_id': 'spy1', 'ls_downtimed': False, 'ls_acknowledged': False, 'active_checks_enabled': True, 'passive_checks_enabled': True, '_overall_state_id': 4, 'ls_state': 'DOWN' }, 'hostname') data_manager.update_database('host', [host_test]) # Spy this host and set current row under_test.spy_list_widget.add_spy_host(host_test.item_id) under_test.spy_list_widget.setCurrentRow(0) # "(new !)" is added to new spy host self.assertEqual( 'Host Hostname, current state: DOWN (new !)', under_test.spy_list_widget.item(0).data(Qt.DisplayRole)) # Manage problems with a valid row under_test.manage_host_events(under_test.spy_list_widget.currentRow()) # Host list equal to 1, No services are attached to host self.assertEqual(1, under_test.host_list_widget.count()) self.assertEqual( 'Hostname is DOWN. No services.', under_test.host_list_widget.item(0).data(Qt.DisplayRole)) # "(new !)" is removed self.assertEqual( 'Host Hostname, current state: DOWN ', under_test.spy_list_widget.item(0).data(Qt.DisplayRole)) # Fill "services" database attached to host service = Service() service.create( '_id1', { 'host': 'spy1', 'ls_state': 'CRITICAL', 'ls_acknowledged': False, 'ls_downtimed': False, 'ls_output': 'Output of service 1', 'ls_last_check': 1234567890, 'passive_checks_enabled': True, 'active_checks_enabled': False }, 'service_name') service_2 = Service() service_2.create( '_id2', { 'host': 'spy1', 'ls_state': 'OK', 'ls_acknowledged': False, 'ls_downtimed': False, 'ls_output': 'Output of service 2', 'ls_last_check': 1234567890, 'passive_checks_enabled': True, 'active_checks_enabled': False }, 'service2_name') data_manager.update_database('service', [service, service_2]) # Manage problems again under_test.manage_host_events(under_test.spy_list_widget.currentRow()) # Host list equal to 1, cause one service is CRITICAL self.assertEqual(1, under_test.host_list_widget.count()) self.assertEqual( 'Service Service_Name is CRITICAL', under_test.host_list_widget.item(0).data(Qt.DisplayRole)) # If CRITICAL service is removed, text change data_manager.remove_item('service', '_id1') # Manage problems again under_test.manage_host_events(under_test.spy_list_widget.currentRow()) # Host list equal to 1, cause one service is CRITICAL self.assertEqual(1, under_test.host_list_widget.count()) self.assertEqual( 'Hostname is DOWN. Services of host seems managed.', under_test.host_list_widget.item(0).data(Qt.DisplayRole))
import unittest2 from PyQt5.Qt import QApplication from alignak_app.backend.datamanager import data_manager from alignak_app.items.host import Host from alignak_app.items.service import Service from alignak_app.items.user import User from alignak_app.utils.config import settings from alignak_app.locales.locales import init_localization from alignak_app.qobjects.host.host import HostQWidget init_localization() data_manager.database['user'] = User() data_manager.database['user'].create('_id', {}, 'name') host = Host() host.create( '_id1', { 'name': 'localhost', 'ls_state': 'DOWN', 'ls_acknowledged': False, 'ls_downtimed': False, }, 'localhost' ) data_manager.database['host'] = [host] class TestHostQWidget(unittest2.TestCase): """
class TestProblemsQTableView(unittest2.TestCase): """ This file test the ProblemsQTableView class. """ # Host data test host_list = [] for i in range(0, 10): host = Host() host.create( '_id%d' % i, { 'name': 'host%d' % i, 'alias': 'Host %d' % i, '_id': '_id%d' % i, 'ls_downtimed': True, 'ls_acknowledged': True, 'ls_state': 'UNKNOWN', 'ls_output': 'output host %d' % i }, 'host%d' % i ) host_list.append(host) # Service data test service_list = [] for i in range(0, 10): service = Service() service.create( '_id%d' % i, { 'name': 'service%d' % i, 'alias': 'Service %d' % i, 'host': '_id%d' % i, '_id': '_id%d' % i, 'ls_acknowledged': False, 'ls_downtimed': False, 'ls_state': 'CRITICAL', 'ls_output': 'output host %d' % i, 'aggregation': 'disk', '_overall_state_id': 4 }, 'service%d' % i ) service_list.append(service) def test_initialize_tables_problems(self): """Initialize ProblemsQTableWidget""" problems = data_manager.get_problems() under_test = ProblemsQTableView() self.assertIsNone(under_test.model()) self.assertIsNone(under_test.selectionModel()) under_test.update_view(problems) self.assertIsNotNone(under_test.model()) self.assertIsNotNone(under_test.selectionModel()) self.assertEqual(['Items in problem', 'Output'], under_test.headers_list) self.assertEqual(2, under_test.model().columnCount()) def test_get_tableitem(self): """Get Problems Table Item""" under_test = ProblemsQTableView() tableitem_test = under_test.get_tableitem(self.host_list[0]) self.assertIsInstance(tableitem_test, QStandardItem) self.assertEqual('Host 0 is UNKNOWN', tableitem_test.text()) tableitem_test = under_test.get_tableitem(self.service_list[0]) self.assertIsInstance(tableitem_test, QStandardItem) self.assertEqual('Service 0 is CRITICAL (Attached to Host 0)', tableitem_test.text())
class TestAllItems(unittest2.TestCase): """ This file test methods of ItemModel class objects """ settings.init_config() init_localization() app_backend.login() # Host data test host_list = [] for i in range(0, 10): host = Host() host.create( '_id%d' % i, { 'name': 'host%d' % i, 'ls_downtimed': True, 'ls_acknowledged': True, 'ls_state': 'UNKNOWN', '_overall_state_id': 4, 'passive_checks_enabled': False, 'active_checks_enabled': True }, 'host%d' % i) host_list.append(host) # Service data test service_list = [] for i in range(0, 10): service = Service() service.create( '_id%d' % i, { 'name': 'service%d' % i, 'alias': 'Service %d' % i, 'host': '_id%d' % i, 'ls_acknowledged': False, 'ls_downtimed': False, 'ls_state': 'CRITICAL', 'aggregation': 'disk', '_overall_state_id': 4, 'passive_checks_enabled': False, 'active_checks_enabled': True }, 'service%d' % i) service_list.append(service) def test_item_model(self): """Create ItemModel""" under_test = Item() under_test.create('_id', {'ls_state': 'DOWN'}, 'name') self.assertTrue('_id' == under_test.item_id) self.assertTrue('ls_state' in under_test.data) self.assertTrue('DOWN' == under_test.data['ls_state']) self.assertTrue('name' == under_test.name) def test_item_model_get_data(self): """Get Data ItemModel""" under_test = Item() under_test.create('_id', { 'ls_state': 'DOWN', 'ls_acknowledged': True }, 'name') data_test = under_test.data['ls_state'] self.assertTrue('DOWN' == data_test) def test_item_model_update_data(self): """Update Data ItemModel""" under_test = Item() under_test.create('_id', { 'ls_state': 'DOWN', 'ls_acknowledged': True }, 'name') under_test.update_data('ls_acknowledged', False) data_test = under_test.data['ls_acknowledged'] self.assertTrue(data_test is False) def test_get_icon_name(self): """Get Icon Name""" under_test = get_icon_name('host', 'UP', acknowledge=False, downtime=False, monitored=1) self.assertEqual('hosts_up', under_test) under_test = get_icon_name('service', 'WARNING', acknowledge=False, downtime=False, monitored=1) self.assertEqual('services_warning', under_test) under_test = get_icon_name('host', 'DOWN', acknowledge=True, downtime=False, monitored=1) self.assertEqual('acknowledge', under_test) under_test = get_icon_name('service', 'UNREACHABLE', acknowledge=True, downtime=True, monitored=2) self.assertEqual('downtime', under_test) under_test = get_icon_name('host', 'WRONG_STATUS', acknowledge=False, downtime=False, monitored=1) self.assertEqual('error', under_test) under_test = get_icon_name('host', 'UP', acknowledge=False, downtime=False, monitored=False + False) self.assertEqual('hosts_not_monitored', under_test) def test_get_icon_name_from_state(self): """Get Icon Name from State""" under_test = get_icon_name_from_state('host', 'UP') self.assertEqual('hosts_up', under_test) under_test = get_icon_name_from_state('service', 'CRITICAL') self.assertEqual('services_critical', under_test) under_test = get_icon_name_from_state('host', 'ACKNOWLEDGE') self.assertEqual('acknowledge', under_test) under_test = get_icon_name_from_state('service', 'DOWNTIME') self.assertEqual('downtime', under_test) def test_get_real_host_state_icon(self): """Get Real Host State Icon""" # Service data test services_test = [] for i in range(0, 5): service = Service() service.create('_id%d' % i, { 'name': 'service%d' % i, '_overall_state_id': i }, 'service%d' % i) services_test.append(service) service = Service() service.create('other_id2%d' % i, { 'name': 'other_service2%d' % i, '_overall_state_id': i }, 'other_service%d' % i) services_test.append(service) under_test = get_overall_state_icon(services_test, 0) self.assertEqual('all_services_critical', under_test) # Overall state id of 10 does not exist under_test = get_overall_state_icon([], 10) self.assertEqual('all_services_none', under_test) def test_get_request_history_model(self): """Get History Request Model""" under_test = History.get_request_model() self.assertTrue('endpoint' in under_test) self.assertEqual('history', under_test['endpoint']) self.assertTrue('params' in under_test) self.assertTrue('projection' in under_test) def test_get_history_icon_name_from_message(self): """Get History Icon from State""" under_test = History.get_history_icon_name('UNKNOWN', 'downtime') self.assertEqual('downtime', under_test) under_test = History.get_history_icon_name('UP', 'ack') self.assertEqual('acknowledge', under_test) under_test = History.get_history_icon_name('UP', 'event_type') self.assertEqual('hosts_up', under_test) under_test = History.get_history_icon_name('DOWN', 'event_type') self.assertEqual('hosts_down', under_test) under_test = History.get_history_icon_name('UNREACHABLE', 'event_type') self.assertEqual('services_unreachable', under_test) under_test = History.get_history_icon_name('OK', 'event_type') self.assertEqual('services_ok', under_test) under_test = History.get_history_icon_name('WARNING', 'event_type') self.assertEqual('services_warning', under_test) under_test = History.get_history_icon_name('CRITICAL', 'event_type') self.assertEqual('services_critical', under_test) under_test = History.get_history_icon_name('UNKNOWN', 'event_type') self.assertEqual('services_unknown', under_test) under_test = History.get_history_icon_name('error', 'event_type') self.assertEqual('error', under_test) def test_get_request_user_model(self): """Get User Request Model""" under_test = User.get_request_model('') self.assertTrue('endpoint' in under_test) self.assertEqual('user', under_test['endpoint']) self.assertTrue('params' in under_test) self.assertTrue('projection' in under_test) def test_get_user_role(self): """Get User Role""" # User case user_test = User() user_test.create( '_id', { 'is_admin': False, 'can_submit_commands': False, 'back_role_super_admin': False }, 'name') under_test = user_test.get_role() self.assertEqual('user', under_test) # Administrator case user_test = User() user_test.create( '_id', { 'is_admin': True, 'can_submit_commands': False, 'back_role_super_admin': False }, 'name') under_test = user_test.get_role() self.assertEqual('administrator', under_test) # Power case user_test = User() user_test.create( '_id', { 'is_admin': False, 'can_submit_commands': True, 'back_role_super_admin': False }, 'name') under_test = user_test.get_role() self.assertEqual('power', under_test) def test_get_request_host_model(self): """Get Host Request Model""" under_test = Host.get_request_model() self.assertTrue('endpoint' in under_test) self.assertEqual('host', under_test['endpoint']) self.assertTrue('params' in under_test) self.assertTrue('projection' in under_test) def test_get_overall_tooltip(self): """Get Overall Tooltip of Host""" under_test = Host() under_test.create( '_id1', { '_overall_state_id': 1, 'ls_state': 'DOWN', 'ls_downtimed': True, 'ls_acknowledged': False, 'active_checks_enabled': True, 'passive_checks_enabled': False, }, 'hostname') tooltip_test = under_test.get_overall_tooltip([]) self.assertEqual('Hostname is DOWN but downtimed, no services...', tooltip_test) def test_get_request_service_model(self): """Get Service Request Model""" under_test = Service.get_request_model() self.assertTrue('endpoint' in under_test) self.assertEqual('service', under_test['endpoint']) self.assertTrue('params' in under_test) self.assertTrue('projection' in under_test) def test_get_request_daemon_model(self): """Get Daemon Request Model""" under_test = Daemon.get_request_model() self.assertTrue('endpoint' in under_test) self.assertEqual('alignakdaemon', under_test['endpoint']) self.assertTrue('params' in under_test) self.assertTrue('projection' in under_test) def test_get_daemons_names(self): """Get All Daemon Names""" daemon_names = [ 'poller', 'receiver', 'reactionner', 'arbiter', 'scheduler', 'broker' ] self.assertEqual(daemon_names, Daemon.get_daemons_names()) def test_get_request_event_model(self): """Get Event Request Model""" under_test = Event.get_request_model() self.assertTrue('endpoint' in under_test) self.assertEqual('history', under_test['endpoint']) self.assertTrue('params' in under_test) self.assertTrue('projection' in under_test) def test_get_request_livesynthesis_model(self): """Get LiveSynthesis Request Model""" under_test = LiveSynthesis.get_request_model() self.assertTrue('endpoint' in under_test) self.assertEqual('livesynthesis', under_test['endpoint']) self.assertTrue('params' in under_test) self.assertTrue('projection' in under_test) def get_request_realm_model(self): """get Realm Request Model""" under_test = Realm.get_request_model() self.assertTrue('endpoint' in under_test) self.assertEqual('realm', under_test['endpoint']) self.assertTrue('params' in under_test) self.assertTrue('projection' in under_test)
def test_update_problems(self): """Update Problems in Database""" under_test = DataManager() host_test = Host() host_test.create( 'id_1', { 'name': 'host_1', 'ls_state': 'UP', 'ls_acknowledged': False, 'ls_downtimed': False, 'passive_checks_enabled': True, 'active_checks_enabled': True }, 'host_1') service_test = Service() service_test.create( 'id_2', { 'name': 'service_1', 'host': 'id_1', 'ls_state': 'OK', 'ls_acknowledged': False, 'ls_downtimed': False, 'passive_checks_enabled': True, 'active_checks_enabled': False }, 'service_1') under_test.database['host'].append(host_test) under_test.database['service'].append(service_test) under_test.update_problems() # Items are not problems, so not in problems database self.assertFalse(host_test in under_test.database['problems']) self.assertFalse(service_test in under_test.database['problems']) # Update the host to make it a problem under_test.update_item_data('host', 'id_1', {'ls_state': 'DOWN'}) under_test.update_problems() # Host is added but not the service self.assertTrue(host_test in under_test.database['problems']) self.assertFalse(service_test in under_test.database['problems']) # Update the service to make it a problem, update host in host database under_test.update_item_data('service', 'id_2', {'ls_state': 'CRITICAL'}) under_test.update_item_data('host', 'id_1', {'ls_state': 'OK'}) under_test.update_problems() # Services is added in problems and host have been removed from problems self.assertFalse(host_test in under_test.database['problems']) self.assertTrue(service_test in under_test.database['problems']) # Add host with same _id in problems, but DOWN and acknowledged host_test_2 = Host() host_test_2.create( 'id_1', { 'name': 'host_1', 'ls_state': 'DOWN', 'ls_acknowledged': True, 'ls_downtimed': False, 'passive_checks_enabled': True, 'active_checks_enabled': False }, 'host_1') under_test.database['problems'].append(host_test_2) # New data for host in "host" database, DOWN but NOT acknowledged under_test.update_item_data('host', 'id_1', {'ls_state': 'DOWN'}) under_test.update_problems() # Host should be in "problems" database, and not ACK. self.assertTrue(under_test.get_item('problems', 'id_1')) self.assertEqual( under_test.get_item('host', 'id_1').data['ls_acknowledged'], under_test.get_item('problems', 'id_1').data['ls_acknowledged'], ) # Service still in problems self.assertTrue(service_test in under_test.database['problems'])
class TestServiceDataQWidget(unittest2.TestCase): """ This file test methods of ServiceDataQWidget class object """ # Host data test host_list = [] for i in range(0, 10): host = Host() host.create( '_id%d' % i, { 'name': 'host%d' % i, '_id': '_id%d' % i, 'passive_checks_enabled': False, 'active_checks_enabled': True }, 'host%d' % i) host_list.append(host) # Service data test service_list = [] for i in range(0, 10): service = Service() service.create( '_id%d' % i, { 'name': 'service%d' % i, 'alias': 'service %d' % i, '_id': '_id%d' % i, 'host': '_id%d' % i, 'ls_acknowledged': False, 'ls_downtimed': False, 'ls_state': 'OK', 'aggregation': 'disk', 'ls_last_check': 123456789, 'ls_output': 'All is ok', 'business_impact': 2, 'passive_checks_enabled': False, 'active_checks_enabled': True }, 'service%d' % i) service_list.append(service) service = Service() service.create( 'other_id2%d' % i, { 'name': 'other_service2%d' % i, '_id': 'other_id2%d' % i, 'host': '_id%d' % i, 'ls_acknowledged': False, 'ls_downtimed': False, 'ls_state': 'CRITICAL', 'aggregation': 'CPU', 'passive_checks_enabled': False, 'active_checks_enabled': True }, 'other_service%d' % i) service_list.append(service) @classmethod def setUpClass(cls): """Create QApplication""" try: cls.app = QApplication(sys.argv) except: pass def test_initialize(self): """Initialize ServiceDataQWidget""" under_test = ServiceDataQWidget() self.assertIsNone(under_test.service_item) self.assertIsNotNone(under_test.labels) for label in under_test.labels: self.assertIsInstance(under_test.labels[label], QLabel) self.assertIsNotNone(under_test.buttons) for button in under_test.buttons: self.assertIsInstance(under_test.buttons[button], QPushButton) under_test.initialize() self.assertIsNone(under_test.service_item) self.assertIsNotNone(under_test.labels) for label in under_test.labels: self.assertIsInstance(under_test.labels[label], QLabel) self.assertIsNotNone(under_test.buttons) for button in under_test.buttons: self.assertIsInstance(under_test.buttons[button], QPushButton) # Assert QWidget is Hidden for first display self.assertTrue(under_test.isHidden()) def test_get_icon_widget(self): """Get Icon QWidget ServiceDataQWidget""" service_data_widget_test = ServiceDataQWidget() under_test = service_data_widget_test.get_service_icon_widget() self.assertIsInstance(under_test, QWidget) def test_update_widget(self): """Update ServiceData QWidget""" under_test = ServiceDataQWidget() under_test.initialize() old_labels = {} # Store QLabel.text() = '' for label in under_test.labels: old_labels[label] = under_test.labels[label] data_manager.database['user'].data['can_submit_commands'] = True data_manager.update_database('service', self.service_list) under_test.update_widget(self.service_list[0]) new_labels = under_test.labels # Assert labels have been filled by update for label in old_labels: self.assertNotEqual(new_labels[label].text(), old_labels[label])
class TestDataManager(unittest2.TestCase): """ This file test the DataManager class. """ # Host data test host_list = [] for i in range(0, 10): host = Host() host.create( '_id%d' % i, { 'name': 'host%d' % i, 'ls_state': 'DOWN', 'ls_acknowledged': False, 'ls_downtimed': False, }, 'host%d' % i) host_list.append(host) # Service data test service_list = [] for i in range(0, 10): service = Service() service.create( '_id%d' % i, { 'name': 'service%d' % i, 'host': '_id%d' % i, 'ls_state': 'CRITICAL', 'ls_acknowledged': False, 'ls_downtimed': False, }, 'service%d' % i) service_list.append(service) service = Service() service.create( 'other_id2%d' % i, { 'name': 'other_service2%d' % i, 'host': '_id%d' % i, 'ls_state': 'UP', 'ls_acknowledged': True, 'ls_downtimed': False, }, 'other_service%d' % i) service_list.append(service) # User data test user = User() user.create('_id', {'name': 'admin'}, 'admin') # Synthesis data test synthesis_data = [{ 'hosts_total': 2, 'hosts_unreachable_hard': 0, '_id': '59c4e40635d17b8e0c6accaf', '_etag': '809a1cf43eaf858de1ef48df38ced9bb5875a3c8', 'services_business_impact': 0, 'hosts_down_hard': 1, 'hosts_in_downtime': 0, 'services_unreachable_soft': 0, 'services_unreachable_hard': 8, 'services_warning_hard': 0, 'hosts_up_hard': 0, 'services_unknown_soft': 0, 'services_acknowledged': 4, 'services_ok_soft': 0, 'hosts_business_impact': 0, 'hosts_acknowledged': 1, '_realm': '59c4e40435d17b8e0c6acc60', '_created': 'Thu, 01 Jan 1970 00:00:00 GMT', 'hosts_unreachable_soft': 0, 'services_in_downtime': 0, '_updated': 'Thu, 01 Jan 1970 00:00:00 GMT', 'services_ok_hard': 1, 'services_total': 14, 'services_critical_soft': 0, 'services_warning_soft': 0, 'hosts_down_soft': 0, 'hosts_up_soft': 0, 'services_critical_hard': 0, 'hosts_flapping': 0, 'services_flapping': 0, 'services_unknown_hard': 1 }, { 'hosts_total': 34, 'hosts_unreachable_hard': 0, '_id': '59c4e40635d17b8e0c6accb0', '_etag': '6999aaa6d1b8ebe867f2f6d55c01a7dc71330f73', 'services_business_impact': 0, 'hosts_down_hard': -7, 'hosts_in_downtime': 0, 'services_unreachable_soft': 0, 'services_unreachable_hard': 71, 'services_warning_hard': 3, 'hosts_up_hard': 39, 'services_unknown_soft': 0, 'services_acknowledged': 76, 'services_ok_soft': 0, 'hosts_business_impact': 0, 'hosts_acknowledged': 2, '_realm': '59c4e38535d17b8dcb0bed42', '_created': 'Thu, 01 Jan 1970 00:00:00 GMT', 'hosts_unreachable_soft': 0, 'services_in_downtime': 0, '_updated': 'Fri, 22 Sep 2017 10:20:54 GMT', 'services_ok_hard': 209, 'services_total': 404, 'services_critical_soft': 1, 'services_warning_soft': 0, 'hosts_down_soft': 0, 'hosts_up_soft': 0, 'services_critical_hard': 26, 'hosts_flapping': 0, 'services_flapping': 0, 'services_unknown_hard': 18 }] livesynth_list = [] for data in synthesis_data: livesynth = LiveSynthesis() livesynth.create(data['_id'], data) livesynth_list.append(livesynth) # Event data test event_data = [{ '_created': 'Thu, 12 Oct 2017 13:27:02 GMT', '_id': '59df6da635d17b0277ddaaed', '_updated': 'Thu, 12 Oct 2017 13:27:02 GMT', '_etag': '70a7fd01040ce20cd84d4059849b548d493e9703', 'message': 'HOST NOTIFICATION: imported_admin;charnay;DOWN;notify-host-by-email;Alarm timeout', 'host': 'host1' }, { '_created': 'Thu, 12 Oct 2017 13:27:02 GMT', '_id': '59df6da635d1j5k77dd3aed', '_updated': 'Thu, 12 Oct 2017 13:27:02 GMT', '_etag': '70a7fd01040ce20c4df459t65g9b548d493e9703', 'message': 'HOST NOTIFICATION: imported_admin;charnay;WARNING;notify-host-by-email;Alarm timeout', 'host': 'host2' }, { '_created': 'Thu, 12 Oct 2017 13:27:02 GMT', '_id': '59df6tg5721j5k77dd3aed', '_updated': 'Thu, 12 Oct 2017 13:27:02 GMT', '_etag': '70a7fd01040ce20c4df459t65g9b548d493e9703', 'message': 'SERVICE: imported_admin;charnay;alarm check;OK;notify-host-by-email;All ok', 'host': 'host3' }] event_list = [] for data in event_data: event = Event() event.create(data['_id'], data) event_list.append(event) # Realm data test realm_list = [] for i in range(0, 10): realm = Realm() realm.create('_id%d' % i, { 'name': 'realm%d' % i, 'alias': 'My Realm %d' % i, }, 'realm%d' % i) realm_list.append(realm) realm_noalias = Realm() realm_noalias.create('_id', { 'name': 'realm', }, 'realm') realm_list.append(realm_noalias) # TimePeriod data test period_list = [] for i in range(0, 10): period = Realm() period.create('_id%d' % i, { 'name': 'period%d' % i, 'alias': 'My Time Period %d' % i, }, 'period%d' % i) period_list.append(period) period_noalias = Period() period_noalias.create('_id', { 'name': 'period', }, 'period') period_list.append(period_noalias) def test_initialize(self): """Initialize DataManager""" under_test = DataManager() self.assertTrue('history' in under_test.database) self.assertTrue('notifications' in under_test.database) self.assertTrue('livesynthesis' in under_test.database) self.assertTrue('alignakdaemon' in under_test.database) self.assertTrue('host' in under_test.database) self.assertTrue('service' in under_test.database) self.assertTrue('user' in under_test.database) self.assertFalse(under_test.old_notifications) self.assertFalse(under_test.ready) self.assertNotEqual('READY', under_test.is_ready()) def test_update_item_database(self): """Update DataManager Database""" under_test = DataManager() under_test.update_database('host', self.host_list) # Assert only "host' databse is filled self.assertFalse(under_test.database['history']) self.assertFalse(under_test.database['notifications']) self.assertFalse(under_test.database['livesynthesis']) self.assertFalse(under_test.database['alignakdaemon']) self.assertFalse(under_test.database['service']) self.assertFalse(under_test.database['user']) self.assertTrue(under_test.database['host']) def test_get_item(self): """Get Item from Database""" under_test = DataManager() under_test.update_database('service', self.service_list) self.assertTrue(under_test.database['service']) # Get item with value item = under_test.get_item('service', 'name', 'service2') self.assertEqual('service2', item.name) self.assertEqual('_id2', item.item_id) self.assertEqual('service2', item.data['name']) # Get item with only key item2 = under_test.get_item('service', 'service3') self.assertEqual('service3', item2.name) self.assertEqual('_id3', item2.item_id) self.assertEqual('service3', item2.data['name']) # Get item who's not here item3 = under_test.get_item('service', 'service10') self.assertIsNone(item3) def test_remove_item(self): """Remove Item from Database""" under_test = DataManager() service_1 = Service() service_1.create('id1', {'name': 'service_one'}, 'name1') service_2 = Service() service_2.create('id2', {'name': 'service_two'}, 'name2') under_test.update_database('service', [service_1, service_2]) # Assert first service is returned service = under_test.get_item('service', service_1.item_id) self.assertTrue(service) # Remove item under_test.remove_item('service', service_1.item_id) # Item is removed service = under_test.get_item('service', service_1.item_id) self.assertFalse(service) def test_get_realm_name(self): """Get Realm in db""" under_test = DataManager() self.assertFalse(under_test.database['realm']) under_test.update_database('realm', self.realm_list) self.assertTrue(under_test.database['realm']) realm_test = under_test.get_realm_name('_id2') self.assertEqual('My Realm 2', realm_test) noalias_realm_test = under_test.get_realm_name('_id') self.assertEqual('Realm', noalias_realm_test) no_realm_test = under_test.get_realm_name('no_realm') self.assertEqual('n/a', no_realm_test) def test_get_period_name(self): """Get Time Period in db""" under_test = DataManager() self.assertFalse(under_test.database['timeperiod']) under_test.update_database('timeperiod', self.period_list) self.assertTrue(under_test.database['timeperiod']) period_test = under_test.get_period_name('_id4') self.assertEqual('My Time Period 4', period_test) noalias_period_test = under_test.get_period_name('_id') self.assertEqual('Period', noalias_period_test) no_period_test = under_test.get_period_name('no_period') self.assertEqual('n/a', no_period_test) def test_get_livesynthesis(self): """Get Livesynthesis in db""" under_test = DataManager() synth_test = under_test.database['livesynthesis'] self.assertFalse(synth_test) under_test.update_database('livesynthesis', self.livesynth_list) self.assertTrue(under_test.database['livesynthesis']) synthesis_count_test = LiveSynthesis.get_synthesis_count_model() livesynthesis = under_test.get_synthesis_count() # Assert Synthesis model is respected for key in synthesis_count_test: self.assertTrue(key in livesynthesis) for state in synthesis_count_test[key]: self.assertTrue(state in livesynthesis[key]) def test_get_all_hotsnames(self): """Gel all Hostnames in db""" under_test = DataManager() under_test.update_database('host', self.host_list) hostnames_test = under_test.get_all_hostnames() self.assertTrue('host0' in hostnames_test) self.assertTrue('host1' in hostnames_test) self.assertTrue('host2' in hostnames_test) self.assertTrue('host3' in hostnames_test) self.assertTrue('host4' in hostnames_test) self.assertTrue('host5' in hostnames_test) self.assertTrue('host6' in hostnames_test) self.assertTrue('host7' in hostnames_test) self.assertTrue('host8' in hostnames_test) self.assertTrue('host9' in hostnames_test) self.assertTrue('host10' not in hostnames_test) def test_get_host_services(self): """Get Services of Host""" under_test = DataManager() under_test.update_database('host', self.host_list) under_test.update_database('service', self.service_list) host_services_test = under_test.get_host_services('_id1') for item in host_services_test: self.assertIsInstance(item, Service) self.assertTrue(item.data['host'] == '_id1') self.assertTrue(2 == len(host_services_test)) def test_get_host_with_services(self): """Get Host with Services""" under_test = DataManager() under_test.update_database('host', self.host_list) under_test.update_database('service', self.service_list) host_with_services_test = under_test.get_host_with_services('host5') for _ in host_with_services_test: self.assertTrue('host' in host_with_services_test) self.assertTrue('services' in host_with_services_test) self.assertTrue(2 == len(host_with_services_test['services'])) self.assertIsInstance(host_with_services_test['host'], Host) for service in host_with_services_test['services']: self.assertIsInstance(service, Service) def test_get_events(self): """Get Events to send""" under_test = DataManager() under_test.update_database('notifications', self.event_list) events = under_test.get_events() self.assertEqual(3, len(events)) for event in events: self.assertTrue('message' in event) self.assertTrue('event_type' in event) if event['event_type'] == 'DOWN': self.assertTrue('DOWN' in event['message']) if event['event_type'] == 'WARNING': self.assertTrue('WARNING' in event['message']) if event['event_type'] == 'OK': self.assertTrue('OK' in event['message']) def test_is_ready(self): """Database is Ready""" under_test = DataManager() self.assertTrue('Collecting' in under_test.is_ready()) under_test.db_is_ready['livesynthesis'] = True self.assertFalse(under_test.ready) self.assertTrue('Collecting' in under_test.is_ready()) under_test.db_is_ready['user'] = True self.assertFalse(under_test.ready) self.assertTrue('Collecting' in under_test.is_ready()) under_test.db_is_ready['realm'] = True self.assertFalse(under_test.ready) self.assertTrue('Collecting' in under_test.is_ready()) under_test.db_is_ready['timeperiod'] = True self.assertFalse(under_test.ready) self.assertTrue('Collecting' in under_test.is_ready()) under_test.db_is_ready['host'] = True self.assertFalse(under_test.ready) self.assertTrue('Collecting' in under_test.is_ready()) under_test.db_is_ready['problems'] = [True, True, True] self.assertFalse(under_test.ready) self.assertTrue('Collecting' in under_test.is_ready()) under_test.db_is_ready['alignakdaemon'] = True self.assertFalse(under_test.ready) self.assertEqual('READY', under_test.is_ready()) self.assertTrue(under_test.ready) def test_update_problems(self): """Update Problems in Database""" under_test = DataManager() host_test = Host() host_test.create( 'id_1', { 'name': 'host_1', 'ls_state': 'UP', 'ls_acknowledged': False, 'ls_downtimed': False, 'passive_checks_enabled': True, 'active_checks_enabled': True }, 'host_1') service_test = Service() service_test.create( 'id_2', { 'name': 'service_1', 'host': 'id_1', 'ls_state': 'OK', 'ls_acknowledged': False, 'ls_downtimed': False, 'passive_checks_enabled': True, 'active_checks_enabled': False }, 'service_1') under_test.database['host'].append(host_test) under_test.database['service'].append(service_test) under_test.update_problems() # Items are not problems, so not in problems database self.assertFalse(host_test in under_test.database['problems']) self.assertFalse(service_test in under_test.database['problems']) # Update the host to make it a problem under_test.update_item_data('host', 'id_1', {'ls_state': 'DOWN'}) under_test.update_problems() # Host is added but not the service self.assertTrue(host_test in under_test.database['problems']) self.assertFalse(service_test in under_test.database['problems']) # Update the service to make it a problem, update host in host database under_test.update_item_data('service', 'id_2', {'ls_state': 'CRITICAL'}) under_test.update_item_data('host', 'id_1', {'ls_state': 'OK'}) under_test.update_problems() # Services is added in problems and host have been removed from problems self.assertFalse(host_test in under_test.database['problems']) self.assertTrue(service_test in under_test.database['problems']) # Add host with same _id in problems, but DOWN and acknowledged host_test_2 = Host() host_test_2.create( 'id_1', { 'name': 'host_1', 'ls_state': 'DOWN', 'ls_acknowledged': True, 'ls_downtimed': False, 'passive_checks_enabled': True, 'active_checks_enabled': False }, 'host_1') under_test.database['problems'].append(host_test_2) # New data for host in "host" database, DOWN but NOT acknowledged under_test.update_item_data('host', 'id_1', {'ls_state': 'DOWN'}) under_test.update_problems() # Host should be in "problems" database, and not ACK. self.assertTrue(under_test.get_item('problems', 'id_1')) self.assertEqual( under_test.get_item('host', 'id_1').data['ls_acknowledged'], under_test.get_item('problems', 'id_1').data['ls_acknowledged'], ) # Service still in problems self.assertTrue(service_test in under_test.database['problems']) def test_get_problems(self): """Get Database Problems""" under_test = DataManager() for item in self.host_list: under_test.database['problems'].append(item) for item in self.service_list: under_test.database['problems'].append(item) problems_test = under_test.get_problems() self.assertEqual(problems_test['hosts_nb'], 10) self.assertEqual(problems_test['services_nb'], 20) self.assertIsNotNone(problems_test['problems']) def test_update_item_data(self): """Update Item Data""" under_test = DataManager() under_test.update_database('host', self.host_list) self.assertEqual('DOWN', under_test.get_item('host', '_id1').data['ls_state']) # Update item data "ls_state" under_test.update_item_data( 'host', '_id1', { 'name': 'host1', 'ls_state': 'UP', 'ls_acknowledged': False, 'ls_downtimed': False, }) self.assertEqual('UP', under_test.get_item('host', '_id1').data['ls_state'])
class TestServicesQWidget(unittest2.TestCase): """ This file test methods of ServicesQWidget class object """ # Host data test host_list = [] for i in range(0, 10): host = Host() host.create( '_id%d' % i, { 'name': 'host%d' % i, 'passive_checks_enabled': False, 'active_checks_enabled': True }, 'host%d' % i) host_list.append(host) # Service data test service_list = [] for i in range(0, 10): service = Service() service.create( '_id%d' % i, { 'name': 'service%d' % i, 'alias': 'Service %d' % i, 'host': '_id%d' % i, 'ls_acknowledged': False, 'ls_downtimed': False, 'ls_state': 'OK', 'aggregation': 'disk', 'passive_checks_enabled': False, 'active_checks_enabled': True }, 'service%d' % i) service_list.append(service) service = Service() service.create( 'other_id2%d' % i, { 'name': 'other_service2%d' % i, 'alias': 'Other Service %d' % i, 'host': '_id%d' % i, 'ls_acknowledged': False, 'ls_downtimed': False, 'ls_state': 'CRITICAL', 'aggregation': 'CPU', 'passive_checks_enabled': False, 'active_checks_enabled': True }, 'other_service%d' % i) service_list.append(service) @classmethod def setUpClass(cls): """Create QApplication""" try: cls.app = QApplication(sys.argv) except Exception as e: print(e) pass def test_initialize(self): """Initialize ServicesQWidget""" under_test = ServicesQWidget() self.assertIsNone(under_test.services) self.assertIsNotNone(under_test.services_tree_widget) self.assertIsNotNone(under_test.service_data_widget) self.assertIsNotNone(under_test.services_dashboard) under_test.initialize() self.assertIsNone(under_test.services) self.assertIsNotNone(under_test.services_tree_widget) self.assertIsNotNone(under_test.service_data_widget) self.assertIsNotNone(under_test.services_dashboard) def test_update_widget(self): """Update Services QWidget""" under_test = ServicesQWidget() data_manager.update_database('host', self.host_list) data_manager.update_database('service', self.service_list) under_test.initialize() self.assertIsNone(under_test.services) services = data_manager.get_host_services(self.host_list[2].item_id) under_test.update_widget(services) self.assertIsNotNone(under_test.services) def test_set_filter_items(self): """Set Filter Services Items""" under_test = ServicesQWidget() data_manager.update_database('host', self.host_list) data_manager.update_database('service', self.service_list) under_test.initialize() services = data_manager.get_host_services(self.host_list[2].item_id) under_test.update_widget(services) self.assertEqual(0, under_test.services_list_widget.count()) under_test.set_filter_items('OK') # Host has only one service OK self.assertEqual(1, under_test.services_list_widget.count()) under_test.services_list_widget.clear() under_test.set_filter_items('UNKNOWN') under_test.services_list_widget.setCurrentItem( under_test.services_list_widget.item(0)) # Host has no service UNKNOWN, so item have hint text self.assertEqual(1, under_test.services_list_widget.count()) self.assertEqual( 'No such services to display...', under_test.services_list_widget.currentItem().data(Qt.DisplayRole)) def test_add_filter_item(self): """Add Filter Service Item to QListWidget""" under_test = ServicesQWidget() under_test.initialize() self.assertEqual(0, under_test.services_list_widget.count()) under_test.add_filter_item(self.service_list[2]) under_test.services_list_widget.setCurrentItem( under_test.services_list_widget.item(0)) # Service "Service 1" is added to QListWidget self.assertEqual(1, under_test.services_list_widget.count()) self.assertEqual( 'Service 1', under_test.services_list_widget.currentItem().data(Qt.DisplayRole))
def update_dashboard(self): """ Update number of items in dashboard """ synthesis = data_manager.get_synthesis_count() hosts_sum = 0 for item in synthesis['hosts']: hosts_sum += synthesis['hosts'][item] services_sum = 0 for item in synthesis['services']: services_sum += synthesis['services'][item] # Hosts percentages self.items_nb['hosts_nb'].setText("%d hosts" % hosts_sum) for icon in Host.get_available_icons(): host_nb = synthesis['hosts'][icon.replace('hosts_', '')] percent = 0.0 try: percent = float(host_nb) * 100.0 / float(hosts_sum) except ZeroDivisionError: pass item_text = '%d (%.02f%%)' % (host_nb, percent) self.hosts_labels[icon].setText(item_text) # Services percentage self.items_nb['services_nb'].setText("%d services" % services_sum) for icon in Service.get_available_icons(): service_nb = synthesis['services'][icon.replace('services_', '')] percent = 0.0 try: percent = float(service_nb) * 100.0 / float(services_sum) except ZeroDivisionError: pass item_text = '%d (%.01f%%)' % (service_nb, percent) self.services_labels[icon].setText(item_text) for button in self.hosts_buttons: if settings.get_config('Alignak', 'webui'): self.hosts_buttons[button].setEnabled(True) self.hosts_buttons[button].setToolTip( _('Hosts %s. See in WebUI ?') % button.replace('hosts_', '').upper() ) else: self.hosts_buttons[button].setToolTip( _("Hosts %s. WebUI is not set in configuration file.") % button.replace( 'hosts_', '').upper() ) for button in self.services_buttons: if settings.get_config('Alignak', 'webui'): self.services_buttons[button].setEnabled(True) self.services_buttons[button].setToolTip( _('Services %s. See in WebUI ?') % button.replace('services_', '').upper() ) else: self.services_buttons[button].setToolTip( _("Services %s. WebUI is not set in configuration file.") % button.replace( 'services_', '').upper() )
import unittest2 from PyQt5.Qt import QApplication from alignak_app.backend.datamanager import data_manager from alignak_app.items.host import Host from alignak_app.items.service import Service from alignak_app.items.user import User from alignak_app.utils.config import settings from alignak_app.locales.locales import init_localization from alignak_app.qobjects.host.host import HostQWidget init_localization() data_manager.database['user'] = User() data_manager.database['user'].create('_id', {}, 'name') host = Host() host.create( '_id1', { 'name': 'localhost', 'ls_state': 'DOWN', 'ls_acknowledged': False, 'ls_downtimed': False, }, 'localhost') data_manager.database['host'] = [host] class TestHostQWidget(unittest2.TestCase): """ This file test methods of AppBackend class """
class TestPanelQWidget(unittest2.TestCase): """ This file test the PanelQWidget class. """ settings.init_config() # Host data test host_list = [] for i in range(0, 10): host = Host() host.create( '_id%d' % i, { 'name': 'host%d' % i, 'alias': 'Host %d' % i, '_id': '_id%d' % i, 'ls_downtimed': False, 'ls_acknowledged': False, 'ls_state': 'UNREACHABLE', 'ls_output': 'output host %d' % i, 'ls_last_check': '', '_realm': '59c4e38535d17b8dcb0bed42', 'address': '127.0.0.1', 'business_impact': '2', 'notes': 'host notes', 'passive_checks_enabled': False, 'active_checks_enabled': True, '_overall_state_id': 1, 'customs': {} }, 'host%d' % i) host_list.append(host) # Service data test service_list = [] for i in range(0, 10): service = Service() service.create( '_id%d' % i, { 'name': 'service%d' % i, 'alias': 'Service %d' % i, 'host': '_id%d' % i, '_id': '_id%d' % i, 'ls_acknowledged': False, 'ls_downtimed': False, 'ls_state': 'CRITICAL', 'ls_output': 'output host %d' % i, 'aggregation': 'disk', '_overall_state_id': 4, 'passive_checks_enabled': False, 'active_checks_enabled': True, }, 'service%d' % i) service_list.append(service) @classmethod def setUpClass(cls): """Create QApplication""" try: cls.app = QApplication(sys.argv) except: pass def test_create_widget(self): """Inititalize PanelQWidget""" # Add problems data_manager.update_database('host', self.host_list) data_manager.database['problems'] = [] for item in self.host_list: data_manager.database['problems'].append(item) for item in self.service_list: data_manager.database['problems'].append(item) for item in self.host_list: assert 'host' in item.item_type under_test = PanelQWidget() self.assertIsNotNone(under_test.layout) self.assertIsNotNone(under_test.dashboard_widget) self.assertIsNotNone(under_test.synthesis_widget) self.assertIsNotNone(under_test.spy_widget) self.assertFalse(under_test.hostnames_list) under_test.initialize() self.assertIsNotNone(under_test.layout) self.assertIsNotNone(under_test.dashboard_widget) self.assertIsNotNone(under_test.synthesis_widget) self.assertIsNotNone(under_test.spy_widget) self.assertEqual([ 'host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9' ], under_test.hostnames_list) def test_spy_host(self): """Panel Add Spy Host""" # init_event_widget() under_test = PanelQWidget() under_test.initialize() # Host is not in hostname_list under_test.synthesis_widget.line_search.setText('no_host') under_test.spy_host() spy_index = under_test.get_tab_order().index('s') self.assertTrue( under_test.synthesis_widget.host_widget.spy_btn.isEnabled()) self.assertEqual('Spy Hosts', under_test.tab_widget.tabText(spy_index)) # Host Id is not added in spied_hosts of SpyQWidget.SpyQListWidget self.assertFalse( '_id0' in under_test.spy_widget.spy_list_widget.spied_hosts) def test_update_panels(self): """Update QTabPanel Problems""" data_manager.database['problems'] = [] data_manager.update_database('host', self.host_list) for item in self.host_list: data_manager.database['problems'].append(item) for item in self.service_list: data_manager.database['problems'].append(item) under_test = PanelQWidget() under_test.initialize() # 20 problems for CRITICAL services and UNREACHABLE hosts problems_index = under_test.get_tab_order().index('p') self.assertEqual('Problems (20)', under_test.tab_widget.tabText(problems_index)) # Remove a service from problems data_manager.database['problems'].remove(self.service_list[0]) under_test.tab_widget.widget(problems_index).update_problems_data() # There are only 9 services in CRITICAL condition self.assertEqual('Problems (19)', under_test.tab_widget.tabText(problems_index)) def test_display_host(self): """Display Host in Panel""" under_test = PanelQWidget() under_test.initialize() self.assertTrue( under_test.synthesis_widget.host_widget.spy_btn.isEnabled()) self.assertEqual( 'Host Synthesis', under_test.tab_widget.tabText( under_test.tab_widget.indexOf(under_test.synthesis_widget))) under_test.display_host() # Host is not spied, so button is enable self.assertTrue( under_test.synthesis_widget.host_widget.spy_btn.isEnabled()) # No customs, so button is not enabled self.assertTrue( under_test.synthesis_widget.host_widget.customs_btn.isEnabled()) # Host and Services Qwidgets are hidden self.assertTrue(under_test.synthesis_widget.host_widget.isHidden()) self.assertTrue(under_test.synthesis_widget.services_widget.isHidden()) # Hint QWidget is shown self.assertFalse(under_test.synthesis_widget.hint_widget.isHidden()) self.assertEqual( 'Host Synthesis', under_test.tab_widget.tabText( under_test.tab_widget.indexOf(under_test.synthesis_widget))) under_test.synthesis_widget.line_search.setText(self.host_list[0].name) under_test.display_host() # Host is not spied, so button is enable self.assertTrue( under_test.synthesis_widget.host_widget.spy_btn.isEnabled()) # No customs, so button is not enabled self.assertFalse( under_test.synthesis_widget.host_widget.customs_btn.isEnabled()) # Host and Services Qwidgets are displayed self.assertFalse(under_test.synthesis_widget.host_widget.isHidden()) self.assertFalse( under_test.synthesis_widget.services_widget.isHidden()) # Hint QWidget is hidden self.assertTrue(under_test.synthesis_widget.hint_widget.isHidden()) self.assertEqual( 'Host "Host 0"', under_test.tab_widget.tabText( under_test.tab_widget.indexOf(under_test.synthesis_widget))) def test_set_host_from_problems(self): """Set Host in Panel from Problems QWidget""" under_test = PanelQWidget() under_test.initialize() self.assertEqual('', under_test.synthesis_widget.line_search.text()) self.assertIsNone( under_test.problems_widget.get_current_user_role_item()) # Make an item as current in problems table under_test.problems_widget.problems_table.update_view( {'problems': [self.host_list[8]]}) index_test = under_test.problems_widget.problems_table.model().index( 0, 0) under_test.problems_widget.problems_table.selectionModel( ).setCurrentIndex(index_test, QItemSelectionModel.SelectCurrent) self.assertIsNotNone( under_test.problems_widget.get_current_user_role_item()) self.assertEqual('', under_test.synthesis_widget.line_search.text()) under_test.set_host_from_problems() # Host is set in line search self.assertEqual('host8', under_test.synthesis_widget.line_search.text())
def test_update_problems(self): """Update Problems in Database""" under_test = DataManager() host_test = Host() host_test.create( 'id_1', { 'name': 'host_1', 'ls_state': 'UP', 'ls_acknowledged': False, 'ls_downtimed': False, 'passive_checks_enabled': True, 'active_checks_enabled': True }, 'host_1' ) service_test = Service() service_test.create( 'id_2', { 'name': 'service_1', 'host': 'id_1', 'ls_state': 'OK', 'ls_acknowledged': False, 'ls_downtimed': False, 'passive_checks_enabled': True, 'active_checks_enabled': False }, 'service_1' ) under_test.database['host'].append(host_test) under_test.database['service'].append(service_test) under_test.update_problems() # Items are not problems, so not in problems database self.assertFalse(host_test in under_test.database['problems']) self.assertFalse(service_test in under_test.database['problems']) # Update the host to make it a problem under_test.update_item_data('host', 'id_1', {'ls_state': 'DOWN'}) under_test.update_problems() # Host is added but not the service self.assertTrue(host_test in under_test.database['problems']) self.assertFalse(service_test in under_test.database['problems']) # Update the service to make it a problem, update host in host database under_test.update_item_data('service', 'id_2', {'ls_state': 'CRITICAL'}) under_test.update_item_data('host', 'id_1', {'ls_state': 'OK'}) under_test.update_problems() # Services is added in problems and host have been removed from problems self.assertFalse(host_test in under_test.database['problems']) self.assertTrue(service_test in under_test.database['problems']) # Add host with same _id in problems, but DOWN and acknowledged host_test_2 = Host() host_test_2.create( 'id_1', { 'name': 'host_1', 'ls_state': 'DOWN', 'ls_acknowledged': True, 'ls_downtimed': False, 'passive_checks_enabled': True, 'active_checks_enabled': False }, 'host_1' ) under_test.database['problems'].append(host_test_2) # New data for host in "host" database, DOWN but NOT acknowledged under_test.update_item_data('host', 'id_1', {'ls_state': 'DOWN'}) under_test.update_problems() # Host should be in "problems" database, and not ACK. self.assertTrue(under_test.get_item('problems', 'id_1')) self.assertEqual( under_test.get_item('host', 'id_1').data['ls_acknowledged'], under_test.get_item('problems', 'id_1').data['ls_acknowledged'], ) # Service still in problems self.assertTrue(service_test in under_test.database['problems'])