コード例 #1
0
class TestBasic(unittest2.TestCase):
    def setUp(self):
        self.dmg = DataManager(alignak_webui.app.app)
        print('Data manager', self.dmg)

        # Initialize and load ... no reset
        assert self.dmg.user_login('admin', 'admin')
        result = self.dmg.load()

    def tearDown(self):
        # Logout
        self.dmg.reset(logout=True)
        assert not self.dmg.my_backend.connected
        assert self.dmg.logged_in_user is None
        assert self.dmg.loaded == False

    def test_5_1_get_simple(self):
        """ Datamanager objects get - simple elements """
        print('test objects get simple objects')

        # Get realms
        items = self.dmg.get_realms()
        for item in items:
            print("Got: ", item)
            assert item.id
            item.get_html_state()
        assert len(items) == 4

        # Get commands
        items = self.dmg.get_commands()
        for item in items:
            print("Got: ", item)
            assert item.id
            icon_status = item.get_html_state()
        assert len(items) == 50  # Backend pagination limit ...

        # Get timeperiods
        items = self.dmg.get_timeperiods()
        for item in items:
            print("Got: ", item)
            assert item.id
            item.get_html_state()
        assert len(items) == 5

    def test_5_1_get_linked(self):
        """ Datamanager objects get - linked elements """
        print('test objects get linked')

        # Get hosts
        items = self.dmg.get_hosts()
        for item in items:
            print("Got: ", item)
            assert item.id
            assert isinstance(item.check_command, Command)  # Must be an object
            assert isinstance(item.check_period,
                              TimePeriod)  # Must be an object
        assert len(items) == 13

        # Get services
        items = self.dmg.get_services()
        for item in items:
            print("Got: ", item)
            assert item.id
            assert isinstance(item.check_command, Command)  # Must be an object
            assert isinstance(item.check_period,
                              TimePeriod)  # Must be an object
        assert len(items) == 50  # Backend pagination limit ...

    def test_5_1_get_linked_groups(self):
        """ Datamanager objects get - group elements """
        print('test objects get self linked')

        # Get hostgroups
        items = self.dmg.get_hostgroups()
        for item in items:
            print("Got: ", item)
            assert item.id
            if item.level != 0:
                assert isinstance(item._parent, HostGroup)  # Must be an object
        assert len(items) == 12

        # Get servicegroups
        items = self.dmg.get_servicegroups()
        for item in items:
            print("Got: ", item)
            assert item.id
            if item.level != 0:
                assert isinstance(item._parent,
                                  ServiceGroup)  # Must be an object
        assert len(items) == 10

        # Get usergroups
        items = self.dmg.get_usergroups()
        for item in items:
            print("Got: ", item)
            assert item.id
            if item.level != 0:
                assert isinstance(item._parent, UserGroup)  # Must be an object
        assert len(items) == 7

    def test_5_3_livesynthesis(self):
        """ Datamanager objects get - livesynthesis """
        print('test livesynthesis')

        self.maxDiff = None

        # Get livesynthesis - user logged-in realm and its sub-realms
        expected_ls = {
            '_id': self.dmg.my_ls['_id'],
            'hosts_synthesis': {
                'nb_elts': 7,
                'nb_not_monitored': 0,
                'pct_not_monitored': 0.0,
                # 'business_impact': 0,
                'warning_threshold': 2.0,
                'global_warning_threshold': 2.0,
                'critical_threshold': 5.0,
                'global_critical_threshold': 5.0,
                'nb_up': 0,
                'pct_up': 0.0,
                'nb_up_hard': 0,
                'nb_up_soft': 0,
                'nb_down': 0,
                'pct_down': 0.0,
                'nb_down_hard': 0,
                'nb_down_soft': 0,
                'nb_unreachable': 7,
                'pct_unreachable': 100.0,
                'nb_unreachable_hard': 7,
                'nb_unreachable_soft': 0,
                'nb_problems': 7,
                'pct_problems': 100.0,
                'nb_flapping': 0,
                'pct_flapping': 0.0,
                'nb_acknowledged': 0,
                'pct_acknowledged': 0.0,
                'nb_in_downtime': 0,
                'pct_in_downtime': 0.0,
            },
            'services_synthesis': {
                'nb_elts': 241,
                'nb_not_monitored': 0,
                'pct_not_monitored': 0.0,
                # 'business_impact': 0,
                'warning_threshold': 2.0,
                'global_warning_threshold': 2.0,
                'critical_threshold': 5.0,
                'global_critical_threshold': 5.0,
                'nb_ok': 0,
                'pct_ok': 0.0,
                'nb_ok_hard': 0,
                'nb_ok_soft': 0,
                'nb_warning': 0,
                'pct_warning': 0.0,
                'nb_warning_hard': 0,
                'nb_warning_soft': 0,
                'nb_critical': 0,
                'pct_critical': 0.0,
                'nb_critical_hard': 0,
                'nb_critical_soft': 0,
                'nb_unknown': 241,
                'pct_unknown': 100.0,
                'nb_unknown_hard': 241,
                'nb_unknown_soft': 0,
                'nb_unreachable': 0,
                'pct_unreachable': 0.0,
                'nb_unreachable_hard': 0,
                'nb_unreachable_soft': 0,
                'nb_problems': 0,
                'pct_problems': 0.0,
                'nb_flapping': 0,
                'pct_flapping': 0.0,
                'nb_acknowledged': 0,
                'pct_acknowledged': 0.0,
                'nb_in_downtime': 0,
                'pct_in_downtime': 0.0
            }
        }
        got_ls = self.dmg.get_livesynthesis({
            'concatenation': '1',
            'where': {
                '_realm': self.dmg.my_realm.id
            }
        })
        assert got_ls['hosts_synthesis'] == expected_ls['hosts_synthesis']
        assert got_ls['services_synthesis'] == expected_ls[
            'services_synthesis']

        # Get livesynthesis - user logged-in realm and no sub realms
        expected_ls = {
            '_id': self.dmg.my_ls['_id'],
            'hosts_synthesis': {
                'nb_elts': 7,
                'nb_not_monitored': 0,
                'pct_not_monitored': 0.0,
                # 'business_impact': 0,
                'warning_threshold': 2.0,
                'global_warning_threshold': 2.0,
                'critical_threshold': 5.0,
                'global_critical_threshold': 5.0,
                'nb_up': 0,
                'pct_up': 0.0,
                'nb_up_hard': 0,
                'nb_up_soft': 0,
                'nb_down': 0,
                'pct_down': 0.0,
                'nb_down_hard': 0,
                'nb_down_soft': 0,
                'nb_unreachable': 7,
                'pct_unreachable': 100.0,
                'nb_unreachable_hard': 7,
                'nb_unreachable_soft': 0,
                'nb_problems': 7,
                'pct_problems': 100.0,
                'nb_flapping': 0,
                'pct_flapping': 0.0,
                'nb_acknowledged': 0,
                'pct_acknowledged': 0.0,
                'nb_in_downtime': 0,
                'pct_in_downtime': 0.0,
            },
            'services_synthesis': {
                'nb_elts': 241,
                'nb_not_monitored': 0,
                'pct_not_monitored': 0.0,
                # 'business_impact': 0,
                'warning_threshold': 2.0,
                'global_warning_threshold': 2.0,
                'critical_threshold': 5.0,
                'global_critical_threshold': 5.0,
                'nb_ok': 0,
                'pct_ok': 0.0,
                'nb_ok_hard': 0,
                'nb_ok_soft': 0,
                'nb_warning': 0,
                'pct_warning': 0.0,
                'nb_warning_hard': 0,
                'nb_warning_soft': 0,
                'nb_critical': 0,
                'pct_critical': 0.0,
                'nb_critical_hard': 0,
                'nb_critical_soft': 0,
                'nb_unknown': 241,
                'pct_unknown': 100.0,
                'nb_unknown_hard': 241,
                'nb_unknown_soft': 0,
                'nb_unreachable': 0,
                'pct_unreachable': 0.0,
                'nb_unreachable_hard': 0,
                'nb_unreachable_soft': 0,
                'nb_problems': 0,
                'pct_problems': 0.0,
                'nb_flapping': 0,
                'pct_flapping': 0.0,
                'nb_acknowledged': 0,
                'pct_acknowledged': 0.0,
                'nb_in_downtime': 0,
                'pct_in_downtime': 0.0
            }
        }
        got_ls = self.dmg.get_livesynthesis(self.dmg.my_ls['_id'])
        assert got_ls['hosts_synthesis'] == expected_ls['hosts_synthesis']
        assert got_ls['services_synthesis'] == expected_ls[
            'services_synthesis']
コード例 #2
0
class TestHosts(unittest2.TestCase):
    def setUp(self):
        print("setting up ...")
        self.dmg = DataManager(alignak_webui.app.app)
        print('Data manager', self.dmg)

        # Initialize and do not load
        assert self.dmg.user_login('admin', 'admin', load=False)

    def tearDown(self):
        print("tearing down ...")
        # Logout
        self.dmg.reset(logout=True)

    def test_hosts(self):
        """ Datamanager objects get - hosts """
        print("Get all hosts")

        # Get main realm
        self.dmg.get_realm({'where': {'name': 'All'}})

        # Get main TP
        self.dmg.get_timeperiod({'where': {'name': '24x7'}})

        # Get all hosts
        hosts = self.dmg.get_hosts()
        assert 13 == len(hosts)
        print("---")
        for host in hosts:
            print("Got host: %s" % host)

        # Get all hosts (really all...)
        hosts = self.dmg.get_hosts(all_elements=True)
        assert 13 == len(hosts)
        print("---")
        for host in hosts:
            print("Got host: %s" % host)

        # Get all hosts (with all embedded relations)
        hosts = self.dmg.get_hosts(embedded=True)
        assert 13 == len(hosts)
        print("---")
        for host in hosts:
            print("Got host: %s" % host)

        # Get all hosts templates
        hosts = self.dmg.get_hosts(template=True)
        assert 28 == len(hosts)
        print("---")
        for host in hosts:
            print("Got host template: %s" % host)

        # Get one host
        hosts = self.dmg.get_hosts({'where': {'name': 'alignak_glpi'}})
        assert 1 == len(hosts)
        print("---")
        for host in hosts:
            print("Got host: %s" % host)
        assert hosts[0].name == 'alignak_glpi'

        # Get one host
        host = self.dmg.get_host({'where': {'name': 'alignak_glpi'}})
        assert host.name == 'alignak_glpi'

        # Get one host
        host = self.dmg.get_host(host._id)
        assert host.name == 'alignak_glpi'
        assert host.status == 'UNREACHABLE'

        # Get host services
        services = self.dmg.get_host_services({'where': {'name': 'unknown'}})
        assert services == -1

        services = self.dmg.get_host_services(
            {'where': {
                'name': 'alignak_glpi'
            }})
        print("---")
        service_name = ''
        for service in services:
            print("Got service: %s" % service)
            service_name = service['name']
        assert len(services) > 1
        services = self.dmg.get_host_services(
            {'where': {
                'name': 'alignak_glpi'
            }},
            search={'where': {
                'name': service_name
            }})
        services = self.dmg.get_host_services(host)
        assert len(services) > 1

        # Get host overall state
        (state, status) = self.dmg.get_host_overall_state(host)
        print("Host overall state: %s %s" % (state, status))
        assert state == 3
        assert status == 'warning'

    def test_host(self):
        """ Datamanager objects get - host """
        print("--- test Item")

        # Get main realm
        self.dmg.get_realm({'where': {'name': 'All'}})

        # Get main TP
        self.dmg.get_timeperiod({'where': {'name': '24x7'}})

        # Get host
        host = self.dmg.get_host({'where': {'name': 'localhost'}})

        print(host.__dict__)
        print(host.check_period)
        assert isinstance(host.check_command, Command)
        assert host.check_command
コード例 #3
0
class TestNotAdmin(unittest2.TestCase):
    def setUp(self):
        self.dmg = DataManager(alignak_webui.app.app)
        print('Data manager', self.dmg)

    @unittest2.skip(
        "Skipped because creating a new user do not allow him to get its own data (timeperiod get is 404)!"
    )
    def test_4_1_load(self):
        """ Datamanager load, not admin user """
        print('test load not admin user')

        # Initialize and load ... no reset
        assert self.dmg.user_login('admin', 'admin')
        result = self.dmg.load()
        print("Result:", result)
        assert result == 0  # No new objects created ...

        # Get main realm
        realm_all = self.dmg.get_realm({'where': {'name': 'All'}})

        # Get main TP
        tp_all = self.dmg.get_timeperiod({'where': {'name': '24x7'}})

        # Create a non admin user ...
        # Create a new user
        print('create a user')
        data = {
            "name": "not_admin",
            "alias": "Testing user - not administrator",
            "min_business_impact": 0,
            "email": "*****@*****.**",
            "is_admin": False,
            "expert": False,
            "can_submit_commands": False,
            "host_notifications_enabled": True,
            "host_notification_period": tp_all.id,
            "host_notification_commands": [],
            "host_notification_options": ["d", "u", "r"],
            "service_notifications_enabled": True,
            "service_notification_period": tp_all.id,
            "service_notification_commands": [],
            "service_notification_options": ["w", "u", "c", "r"],
            "definition_order": 100,
            "address1": "",
            "address2": "",
            "address3": "",
            "address4": "",
            "address5": "",
            "address6": "",
            "pager": "",
            "notificationways": [],
            "_realm": realm_all.id
        }
        new_user_id = self.dmg.add_user(data)
        print("New user id: %s" % new_user_id)

        # Logout
        # self.dmg.reset(logout=True)
        # assert not self.dmg.backend.connected
        # assert self.dmg.logged_in_user is None
        # assert self.dmg.loaded == False

        # Login as not_admin created user
        assert self.dmg.user_login('admin', 'admin', load=False)
        print("-----")

        assert self.dmg.user_login('not_admin', 'NOPASSWORDSET', load=False)
        assert self.dmg.my_backend.connected
        assert self.dmg.logged_in_user
        print("Logged-in user: %s" % self.dmg.logged_in_user)
        assert self.dmg.logged_in_user.get_username() == 'not_admin'
        print('logged in as not_admin')

        # Initialize and load ...
        result = self.dmg.load()
        print("Result:", result)
        print("Objects count:", self.dmg.get_objects_count())
        print("Objects count:", self.dmg.get_objects_count('host'))
        print("Objects count:", self.dmg.get_objects_count('service'))
        print("Objects count (log):", self.dmg.get_objects_count(log=True))
        # assert result == 0                          # Only the newly created user, so no new objects loaded
        # assert self.dmg.get_objects_count() == 1    # not_admin user

        # Initialize and load ... with reset
        result = self.dmg.load(reset=True)
        print("Result:", result)
        print("Objects count:", self.dmg.get_objects_count())
        print("Objects count:", self.dmg.get_objects_count('host'))
        print("Objects count:", self.dmg.get_objects_count('service'))
        print("Objects count (log):", self.dmg.get_objects_count(log=True))
        # assert result == 3                          # not_admin user + test_service + relation
        # assert self.dmg.get_objects_count() == 3    # not_admin user + test_service + relation

        # Not admin user can see only its own data, ...
        # -------------------------------------------

        # Do not check the length because the backend contains more elements than needed ...
        # dump_backend(not_admin_user=True, test_service=True)

        # Get users
        items = self.dmg.get_users()
        print("Users:", items)
        # assert len(items) == 1
        # 1 user only ...

        # Get commands
        items = self.dmg.get_commands()
        print("Commands:", items)
        # assert len(items) == 1

        # Get realms
        items = self.dmg.get_realms()
        print("Commands:", items)
        # assert len(items) == 1

        # Get timeperiods
        items = self.dmg.get_timeperiods()
        print("Commands:", items)
        # assert len(items) == 1

        # Get hosts
        items = self.dmg.get_hosts()
        print("Hosts:", items)
        # assert len(items) == 1

        # Get services
        items = self.dmg.get_services()
        print("Services:", items)
        # assert len(items) == 1

        result = self.dmg.delete_user(new_user_id)
        # Cannot delete the current logged-in user
        assert not result

        # Logout
        self.dmg.reset(logout=True)
        assert not self.dmg.my_backend.connected
        assert self.dmg.logged_in_user is None
        assert self.dmg.loaded == False

        # Login as admin
        assert self.dmg.user_login('admin', 'admin', load=False)
        assert self.dmg.my_backend.connected
        assert self.dmg.logged_in_user.get_username() == 'admin'

        result = self.dmg.delete_user(new_user_id)
        # Can delete the former logged-in user
        assert result