Пример #1
0
    def reload_configuration():
        """
        Reload configuration

        """

        logger.info('Reload configuration...')
        init_config()
        send_banner('INFO', 'Configuration reloaded')
Пример #2
0
    def test_get_image_path(self):
        """Get Right Image Path"""
        utils.init_config()

        expected_img = utils.get_main_folder() + '/images/icon.svg'

        under_test = utils.get_image_path('icon')

        self.assertEqual(under_test, expected_img)
Пример #3
0
    def test_app_config(self):
        """Set and Get app_config"""

        # Reset app_config to None
        utils.app_config = None

        self.assertIsNone(utils.app_config)

        utils.init_config()

        self.assertIsNotNone(utils.app_config)
Пример #4
0
class TestAppLogin(unittest2.TestCase):
    """
        This file test the AppQWidget class.
    """

    init_config()

    @classmethod
    def setUpClass(cls):
        """Create QApplication"""
        try:
            cls.app = QApplication(sys.argv)
        except:
            pass

    def test_create_widget(self):
        """Inititalize AppQWidget"""

        under_test = AppQWidget()

        self.assertIsNone(under_test.offset)

        self.assertTrue('app_widget' not in under_test.objectName())
        self.assertTrue('MyTitle' not in under_test.windowTitle())

        under_test.initialize('MyTitle')

        self.assertTrue('app_widget' in under_test.objectName())
        self.assertTrue('MyTitle' in under_test.windowTitle())
Пример #5
0
    def test_reload_config(self):
        """Reload Configuration"""

        # Reset and Init "app_config"
        utils.app_config = None
        self.assertIsNone(utils.app_config)
        utils.init_config()

        cur_config = utils.app_config

        utils.init_config()

        new_config = utils.app_config

        self.assertFalse(cur_config is new_config)
        self.assertTrue(utils.app_config is new_config)
Пример #6
0
class TestAppLogin(unittest2.TestCase):
    """
        This file test the AppLogin class.
    """

    init_config()

    @classmethod
    def setUpClass(cls):
        """Create QApplication"""
        try:
            cls.app = QApplication(sys.argv)
        except:
            pass

    def test_create_widget(self):
        """Inititalize AppLogin"""

        under_test = AppLogin()

        self.assertIsNotNone(under_test.app_backend)
        self.assertIsNone(under_test.username_line)
        self.assertIsNone(under_test.password_line)

        under_test.create_widget()

        self.assertIsNotNone(under_test.app_backend)
        self.assertIsNotNone(under_test.username_line)
        self.assertIsNotNone(under_test.password_line)

    def test_handle_login_good_connection(self):
        """Handle Login: good credentials"""

        under_test = AppLogin()
        under_test.create_widget()

        under_test.username_line.setText('admin')
        under_test.password_line.setText('admin')

        self.assertFalse(under_test.app_backend.user)

        under_test.handle_login()

        self.assertTrue(under_test.app_backend.user)
        self.assertIsNotNone(under_test.app_backend.user['username'])
        self.assertIsNotNone(under_test.app_backend.user['token'])

    def test_handle_login_bad_connection(self):
        """Handle Login: bad credentials"""

        under_test = AppLogin()
        under_test.create_widget()

        under_test.username_line.setText('bad')
        under_test.password_line.setText('bad')

        under_test.handle_login()

        self.assertFalse(under_test.app_backend.user)
Пример #7
0
    def test_set_app_config(self):
        """Reload config"""

        # Reset and Init "app_config"
        utils.app_config = None
        self.assertIsNone(utils.app_config)
        utils.init_config()

        # Get current url
        under_test = utils.get_app_config('Alignak', 'url')
        self.assertEqual('http://demo.alignak.net', under_test)

        # Change url
        utils.set_app_config('Alignak', 'url', 'http://127.0.0.1')
        new_under_test = utils.get_app_config('Alignak', 'url')
        self.assertEqual('http://127.0.0.1', new_under_test)

        # Back url to normal
        utils.set_app_config('Alignak', 'url', 'http://demo.alignak.net')
        last_under_test = utils.get_app_config('Alignak', 'url')

        self.assertEqual('http://demo.alignak.net', last_under_test)
Пример #8
0
class TestServicesView(unittest2.TestCase):
    """
        This file test the Synthesis class.
    """

    init_config()

    app_backend = AppBackend()
    app_backend.login()

    @classmethod
    def setUpClass(cls):
        """Create QApplication"""
        try:
            cls.app = QApplication(sys.argv)
            cls.widget = QWidget()
        except:
            pass

    def test_initialize(self):
        """Initialize Synthesis"""

        under_test = Synthesis()

        self.assertFalse(under_test.app_backend)
        self.assertFalse(under_test.action_manager)
        self.assertIsNone(under_test.host_synthesis)
        self.assertTrue(under_test.line_search)

        under_test.initialize(self.app_backend)

        self.assertTrue(under_test.app_backend)
        self.assertTrue(under_test.action_manager)
        self.assertIsNone(under_test.host_synthesis)
        self.assertTrue(under_test.line_search)

    def test_display_host_synthesis(self):
        """Display Host Synthesis"""

        under_test = Synthesis()

        under_test.initialize(self.app_backend)

        self.assertIsNone(under_test.host_synthesis)
        self.assertTrue(under_test.first_display)

        under_test.display_host_synthesis()

        self.assertIsNotNone(under_test.host_synthesis)
        self.assertFalse(under_test.first_display)
Пример #9
0
            x_w = self.offset.x()
            y_w = self.offset.y()
            self.move(x - x_w, y - y_w)
        except AttributeError as e:
            logger.warning('Move Event %s: %s', self.objectName(), str(e))


if __name__ == '__main__':  # pylint: disable-all
    from PyQt5.QtWidgets import QApplication
    from alignak_app.core.utils import init_config
    import sys

    app = QApplication(sys.argv)
    app.setQuitOnLastWindowClosed(False)

    init_config()

    if 0:
        ack_dialog = Acknowledge()
        ack_dialog.initialize('host', 'pi2', 'Acknowledge requested by App')

        if ack_dialog.exec_() == ack_dialog.Accepted:
            print('Ok')
        else:
            print('Out')
    else:
        downtime_dialog = Downtime()
        downtime_dialog.initialize('host', 'pi2', 'Downtime requested by App')

        if downtime_dialog.exec_() == downtime_dialog.Accepted:
            print('Fixed: ', downtime_dialog.fixed)
Пример #10
0
class TestAppNotifier(unittest2.TestCase):
    """
        This file test the AppNotifier class.
    """

    init_config()

    icon = QIcon(get_image_path('icon'))

    backend = AppBackend()
    backend.login()

    @classmethod
    def setUpClass(cls):
        """Create QApplication"""
        try:
            cls.app = QApplication(sys.argv)
        except:
            pass

    def test_other_objects_do_not_modify_notifier(self):
        """Other objects do not Modify Notifier"""

        # TrayIcon and Dashboard for notifier
        tray_icon = TrayIcon(self.icon)
        dashboard = Dashboard()
        dashboard.initialize()

        under_test = AppNotifier()
        under_test.initialize(self.backend, tray_icon, dashboard)

        self.assertIsNotNone(under_test.tray_icon)
        self.assertIsNotNone(under_test.app_backend)
        self.assertIsNotNone(under_test.dashboard)
        self.assertFalse(under_test.changes)

        tray_icon.build_menu(self.backend, dashboard)

        self.assertIsNotNone(under_test.tray_icon)
        self.assertIsNotNone(under_test.app_backend)
        self.assertIsNotNone(under_test.dashboard)
        self.assertFalse(under_test.changes)

    def test_check_data(self):
        """Check Data modify TrayIcon Actions"""

        # TrayIcon and Dashboard for notifier
        tray_icon = TrayIcon(self.icon)
        dashboard = Dashboard()
        dashboard.initialize()

        under_test = AppNotifier()
        under_test.initialize(self.backend, tray_icon, dashboard)
        tray_icon.build_menu(self.backend, dashboard)

        # Start notifier
        under_test.set_interval()

        # Check Actions are pending
        self.assertEqual(
            'Hosts UP, Wait...',
            under_test.tray_icon.qaction_factory.get('hosts_up').text())
        self.assertEqual(
            'Services OK, Wait...',
            under_test.tray_icon.qaction_factory.get('services_ok').text())

        # Check data...
        under_test.check_data()

        # ...so menu actions should be update
        self.assertNotEqual(
            'Hosts UP, Wait...',
            under_test.tray_icon.qaction_factory.get('hosts_up').text())
        self.assertNotEqual(
            'Services OK, Wait...',
            under_test.tray_icon.qaction_factory.get('services_ok').text())

    def test_states_change(self):
        """States and Notify Changes"""
        self.backend = AppBackend()
        self.backend.login()

        # TrayIcon and Dashboard for notifier
        dashboard = Dashboard()
        dashboard.initialize()

        tray_icon = TrayIcon(self.icon)
        tray_icon.build_menu(self.backend, dashboard)

        # Initialize Notifier
        under_test = AppNotifier()
        under_test.initialize(self.backend, tray_icon, dashboard)

        # Changes are True, first_start is True
        self.assertFalse(under_test.changes)
        self.assertTrue(under_test.first_start)
        self.assertIsNone(under_test.old_synthesis)

    def test_diff_last_state(self):
        """Diff Last Backend SState"""

        self.backend = AppBackend()
        self.backend.login()

        # TrayIcon and Dashboard for notifier
        tray_icon_test = TrayIcon(self.icon)
        dashboard_test = Dashboard()

        notifier_test = AppNotifier()
        notifier_test.app_backend = self.backend
        notifier_test.tray_icon = tray_icon_test
        notifier_test.dashboard = dashboard_test

        # Notifier check data to get first synthesys counts
        notifier_test.check_data()

        # Get synthesis test to test diff between check
        synthesis = self.backend.synthesis_count()
        under_test = notifier_test.diff_last_states(synthesis)

        fields_test = {
            'hosts': {
                'up': 0,
                'downtime': 0,
                'down': 0,
                'acknowledge': 0,
                'unreachable': 0
            },
            'services': {
                'ok': 0,
                'acknowledge': 0,
                'unknown': 0,
                'critical': 0,
                'downtime': 0,
                'unreachable': 0,
                'warning': 0
            }
        }

        self.assertTrue(under_test)
        self.assertTrue('hosts' in under_test)
        self.assertTrue('services' in under_test)

        for state in fields_test['hosts']:
            self.assertTrue(state in under_test['hosts'])
            self.assertTrue(isinstance(under_test['hosts'][state], int))
        for state in fields_test['services']:
            self.assertTrue(state in under_test['services'])
            self.assertTrue(isinstance(under_test['services'][state], int))
Пример #11
0
class TestTrayIcon(unittest2.TestCase):
    """
        This file test the TrayIcon class.
    """

    init_config()

    icon = QIcon(get_image_path('icon'))

    backend = AppBackend()
    backend.login()

    @classmethod
    def setUpClass(cls):
        """Create QApplication"""
        try:
            cls.app = QApplication(sys.argv)
        except:
            pass

    def test_tray_icon(self):
        """Init TrayIcon and QMenu"""
        under_test = TrayIcon(TestTrayIcon.icon)

        self.assertIsInstance(under_test.menu, QMenu)

    def test_host_actions(self):
        """Hosts QActions are created"""
        under_test = TrayIcon(TestTrayIcon.icon)

        self.assertFalse(under_test.qaction_factory.actions)

        under_test.create_hosts_actions()

        self.assertIsInstance(under_test.qaction_factory.get('hosts_up'), QAction)
        self.assertIsInstance(under_test.qaction_factory.get('hosts_down'), QAction)
        self.assertIsInstance(under_test.qaction_factory.get('hosts_unreachable'), QAction)
        self.assertIsInstance(under_test.qaction_factory.get('hosts_acknowledge'), QAction)
        self.assertIsInstance(under_test.qaction_factory.get('hosts_downtime'), QAction)

    def test_services_actions(self):
        """Services QActions are created"""
        under_test = TrayIcon(TestTrayIcon.icon)

        self.assertFalse(under_test.qaction_factory.actions)

        under_test.create_services_actions()

        self.assertIsInstance(under_test.qaction_factory.get('services_ok'), QAction)
        self.assertIsInstance(under_test.qaction_factory.get('services_warning'), QAction)
        self.assertIsInstance(under_test.qaction_factory.get('services_critical'), QAction)
        self.assertIsInstance(under_test.qaction_factory.get('services_unknown'), QAction)
        self.assertIsInstance(under_test.qaction_factory.get('services_acknowledge'), QAction)
        self.assertIsInstance(under_test.qaction_factory.get('services_downtime'), QAction)

    def test_about_action(self):
        """About QAction is created"""
        under_test = TrayIcon(TestTrayIcon.icon)

        self.assertFalse(under_test.qaction_factory.actions)

        under_test.create_about_action()

        self.assertIsNotNone(under_test.qaction_factory)
        self.assertIsInstance(under_test.qaction_factory.get('about'), QAction)

    def test_quit_action(self):
        """Quit QAction is created"""
        under_test = TrayIcon(TestTrayIcon.icon)

        self.assertFalse(under_test.qaction_factory.actions)

        under_test.create_quit_action()

        self.assertIsNotNone(under_test.qaction_factory.get('exit'))
        self.assertIsInstance(under_test.qaction_factory.get('exit'), QAction)

    def test_build_menu(self):
        """Build Menu add QActions"""
        under_test = TrayIcon(TestTrayIcon.icon)
        dashboard_test = Dashboard()

        # Assert no actions in Menu
        self.assertFalse(under_test.menu.actions())
        self.assertIsNone(under_test.app_about)
        self.assertIsNone(under_test.synthesis)
        self.assertIsNone(under_test.alignak_status)
        self.assertIsNotNone(under_test.qaction_factory)

        under_test.build_menu(self.backend, dashboard_test)

        # Assert actions are added in Menu
        self.assertTrue(under_test.menu.actions())
        self.assertIsNotNone(under_test.app_about)
        self.assertIsNotNone(under_test.synthesis)
        self.assertIsNotNone(under_test.alignak_status)
        self.assertIsNotNone(under_test.qaction_factory)

    def test_update_menus_actions(self):
        """Update Menu QActions"""
        under_test = TrayIcon(TestTrayIcon.icon)

        dashboard_test = Dashboard()
        under_test.build_menu(self.backend, dashboard_test)

        self.assertEqual('Hosts UP, Wait...',
                         under_test.qaction_factory.get('hosts_up').text())
        self.assertEqual('Hosts DOWN, Wait...',
                         under_test.qaction_factory.get('hosts_down').text())
        self.assertEqual('Hosts UNREACHABLE, Wait...',
                         under_test.qaction_factory.get('hosts_unreachable').text())

        self.assertEqual('Services OK, Wait...',
                         under_test.qaction_factory.get('services_ok').text())
        self.assertEqual('Services WARNING, Wait...',
                         under_test.qaction_factory.get('services_warning').text())
        self.assertEqual('Services CRITICAL, Wait...',
                         under_test.qaction_factory.get('services_critical').text())
        self.assertEqual('Services UNKNOWN, Wait...',
                         under_test.qaction_factory.get('services_unknown').text())

        synthesis = {
            'hosts': {
                'up': 1,
                'down': 2,
                'unreachable': 3,
                'acknowledge': 4,
                'downtime': 5,
            },
            'services': {
                'ok': 4,
                'warning': 5,
                'critical': 6,
                'unknown': 7,
                'unreachable': 8,
                'acknowledge': 9,
                'downtime': 10,

            }
        }

        under_test.update_menu_actions(synthesis)

        self.assertEqual('Hosts UP (1)',
                         under_test.qaction_factory.get('hosts_up').text())
        self.assertEqual('Hosts DOWN (2)',
                         under_test.qaction_factory.get('hosts_down').text())
        self.assertEqual('Hosts UNREACHABLE (3)',
                         under_test.qaction_factory.get('hosts_unreachable').text())
        self.assertEqual('Hosts ACKNOWLEDGE (4)',
                         under_test.qaction_factory.get('hosts_acknowledge').text())
        self.assertEqual('Hosts DOWNTIME (5)',
                         under_test.qaction_factory.get('hosts_downtime').text())

        self.assertEqual('Services OK (4)',
                         under_test.qaction_factory.get('services_ok').text())
        self.assertEqual('Services WARNING (5)',
                         under_test.qaction_factory.get('services_warning').text())
        self.assertEqual('Services CRITICAL (6)',
                         under_test.qaction_factory.get('services_critical').text())
        self.assertEqual('Services UNKNOWN (7)',
                         under_test.qaction_factory.get('services_unknown').text())
        self.assertEqual('Services UNREACHABLE (8)',
                         under_test.qaction_factory.get('services_unreachable').text())
        self.assertEqual('Services ACKNOWLEDGE (9)',
                         under_test.qaction_factory.get('services_acknowledge').text())
        self.assertEqual('Services DOWNTIME (10)',
                         under_test.qaction_factory.get('services_downtime').text())
Пример #12
0
class TestNotification(unittest2.TestCase):
    """
        This file test the Dashboard class and by same time the DashboardFactory
    """

    init_config()

    @classmethod
    def setUpClass(cls):
        """Create QApplication"""
        try:
            cls.app = QApplication(sys.argv)
        except:
            pass

    def test_initialize_dashboard(self):
        """Initialize Dashboard"""

        under_test = Dashboard()

        self.assertIsNone(under_test.dashboard_type)
        self.assertIsNotNone(under_test.dashboard_factory)

        # Create all the label
        under_test.initialize()

        self.assertEqual('state', under_test.dashboard_type.objectName())
        self.assertIsNotNone(under_test.dashboard_factory)

    def test_display_dashboard(self):
        """Display Dashboard"""

        under_test = Dashboard()

        under_test.initialize()

        self.assertEqual('', under_test.dashboard_type.text())

        # Simulate dicts of states
        synthesis = {
            'hosts': {
                'up': 1,
                'down': 2,
                'unreachable': 3,
                'acknowledge': 4,
                'downtime': 5,
            },
            'services': {
                'ok': 4,
                'warning': 5,
                'critical': 6,
                'unknown': 7,
                'unreachable': 8,
                'acknowledge': 9,
                'downtime': 10,

            }
        }

        # Send a CRITICAL dashboard
        changes = {
            'hosts': {
                'up': 0,
                'down': 0,
                'unreachable': 0,
                'acknowledge': 0,
                'downtime': 0
            },
            'services': {
                'ok': 0,
                'warning': 0,
                'critical': 0,
                'unknown': 0,
                'unreachable': 0,
                'acknowledge': 0,
                'downtime': 0
            }
        }
        under_test.update_dashboard(synthesis, changes)

        self.assertEqual('CRITICAL', under_test.dashboard_type.text())
        self.assertEqual(
            under_test.dashboard_factory.state_data['hosts_up']['state_number'].text(),
            '1'
        )
        self.assertEqual(
            under_test.dashboard_factory.state_data['hosts_up']['progress_bar'].value(),
            6
        )
        self.assertEqual(
            under_test.dashboard_factory.state_data['hosts_up']['diff'].text(),
            ''
        )
        assert 'Background-color: #e74c3c;' in under_test.styleSheet()

    def test_get_style_sheet(self):
        """Get Style Sheet according to States"""

        ok_css = "Background-color: #27ae60;"
        warning_css = "Background-color: #e67e22;"
        critical_css = "Background-color: #e74c3c;"
        none_css = "Background-color: #EEE;"

        under_test = Dashboard()

        css = {
            'OK': ok_css,
            'WARNING': warning_css,
            'CRITICAL': critical_css,
            'NONE': none_css,
        }
        states = ('OK', 'WARNING', 'CRITICAL', 'NONE')

        for state in states:
            expected_css = css[state]
            under_test.set_style_sheet(state)
            current_css = under_test.styleSheet()
            assert expected_css in current_css

    def test_set_position(self):
        """Dashboard Position change"""

        under_test = Dashboard()

        initial_position = under_test.app_widget.pos()

        # Test set_position affect Dashboard
        set_app_config('Dashboard', 'position', 'top:right')
        under_test.set_position()
        self.assertNotEqual(under_test.app_widget.pos(), initial_position)

        # Test new configuration not affect Dashbard position
        new_position = under_test.app_widget.pos()
        set_app_config('Dashboard', 'position', 'top:left')
        self.assertEqual(under_test.app_widget.pos(), new_position)