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 = {}
Example #2
0
    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_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_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())
Example #5
0
    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 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_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))
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
    """

    # Create config for all methods.
    settings.init_config()
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
    """

    # Create config for all methods.
    settings.init_config()

    @classmethod
    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_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'])