Exemplo n.º 1
0
 def test_list_hosts(self):
     switch.add_switch(
         ip='2887583784',
         user=self.user_object,
     )
     switch.add_switch_machine(
         2,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     list_hosts = switch.list_switch_machines_hosts(
         2,
         user=self.user_object,
     )
     expected = {
         'switch_id': 2,
         'id': 1,
         'mac': '28:6e:d4:46:c4:25',
         'switch_ip': '172.29.8.40',
         'machine_id': 1,
         'port': '1',
         'switch_machine_id': 1
     }
     self.assertTrue(
         all(item in list_hosts[0].items()
             for item in expected.items()))
Exemplo n.º 2
0
 def test_patch_switch_machine(self):
     switch.add_switch_machine(
         1,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     switch.patch_switch_machine(
         1,
         1,
         user=self.user_object,
         tag={
             'patched_tag': 'test_patched_tag'
         }
     )
     switch_patch_switch_machine = switch.list_switch_machines(
         1,
         user=self.user_object,
     )
     expected = {'tag': {
         'patched_tag': 'test_patched_tag'}
     }
     self.assertTrue(
         all(item in switch_patch_switch_machine[0].items()
             for item in expected.items())
     )
Exemplo n.º 3
0
 def test_update_switchmachine(self):
     switch.add_switch_machine(
         1,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     switch.update_switchmachine(
         1,
         location='test_location',
         user=self.user_object,
     )
     update_switchmachine = switch.list_switchmachines(
         user=self.user_object,
     )
     expected = {
         'switch_id': 1,
         'id': 1,
         'mac': '28:6e:d4:46:c4:25',
         'location': 'test_location',
         'switch_ip': '0.0.0.0',
         'machine_id': 1,
         'port': '1',
         'switch_machine_id': 1
     }
     self.assertTrue(
         all(item in update_switchmachine[0].items()
             for item in expected.items())
     )
Exemplo n.º 4
0
 def test_update_switchmachine(self):
     switch.add_switch_machine(
         1,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     switch.update_switchmachine(
         1,
         location='test_location',
         user=self.user_object,
     )
     update_switchmachine = switch.list_switchmachines(
         user=self.user_object, )
     expected = {
         'switch_id': 1,
         'id': 1,
         'mac': '28:6e:d4:46:c4:25',
         'location': 'test_location',
         'switch_ip': '0.0.0.0',
         'machine_id': 1,
         'port': '1',
         'switch_machine_id': 1
     }
     self.assertTrue(
         all(item in update_switchmachine[0].items()
             for item in expected.items()))
Exemplo n.º 5
0
 def test_list_hosts(self):
     switch.add_switch(
         ip='2887583784',
         user=self.user_object,
     )
     switch.add_switch_machine(
         2,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     list_hosts = switch.list_switch_machines_hosts(
         2,
         user=self.user_object,
     )
     expected = {
         'switch_id': 2,
         'id': 1,
         'mac': '28:6e:d4:46:c4:25',
         'switch_ip': '172.29.8.40',
         'machine_id': 1,
         'port': '1',
         'switch_machine_id': 1
     }
     self.assertTrue(
         all(item in list_hosts[0].items() for item in expected.items()))
Exemplo n.º 6
0
def set_switch_machines():
    """Set switches and machines.

    .. note::
       --switch_machines_file is the filename which stores all switches
       and machines information.
       each line in fake_switches_files presents one machine.
       the format of each line machine,<switch_ip>,<switch_port>,<vlan>,<mac>
       or switch,<switch_ip>,<switch_vendor>,<switch_version>,
       <switch_community>,<switch_state>
    """
    if not flags.OPTIONS.switch_machines_file:
        print 'flag --switch_machines_file is missing'
        return
    database.init()
    switches, switch_machines = util.get_switch_machines_from_file(
        flags.OPTIONS.switch_machines_file)
    user = user_api.get_user_object(
        setting.COMPASS_ADMIN_EMAIL
    )
    switch_mapping = {}
    for switch in switches:
        added_switch = switch_api.add_switch(
            user, False, **switch
        )
        switch_mapping[switch['ip']] = added_switch['id']
    for switch_ip, machines in switch_machines.items():
        if switch_ip not in switch_mapping:
            print 'switch ip %s not found' % switch_ip
            sys.exit(1)
        switch_id = switch_mapping[switch_ip]
        for machine in machines:
            switch_api.add_switch_machine(
                user, switch_id, False, **machine
            )
Exemplo n.º 7
0
def set_switch_machines():
    """Set switches and machines.

    .. note::
       --switch_machines_file is the filename which stores all switches
       and machines information.
       each line in fake_switches_files presents one machine.
       the format of each line machine,<switch_ip>,<switch_port>,<vlan>,<mac>
       or switch,<switch_ip>,<switch_vendor>,<switch_version>,
       <switch_community>,<switch_state>
    """
    if not flags.OPTIONS.switch_machines_file:
        print 'flag --switch_machines_file is missing'
        return
    database.init()
    switches, switch_machines = util.get_switch_machines_from_file(
        flags.OPTIONS.switch_machines_file)
    user = user_api.get_user_object(setting.COMPASS_ADMIN_EMAIL)
    switch_mapping = {}
    for switch in switches:
        added_switch = switch_api.add_switch(False, user=user, **switch)
        switch_mapping[switch['ip']] = added_switch['id']
    for switch_ip, machines in switch_machines.items():
        if switch_ip not in switch_mapping:
            print 'switch ip %s not found' % switch_ip
            sys.exit(1)
        switch_id = switch_mapping[switch_ip]
        for machine in machines:
            switch_api.add_switch_machine(switch_id,
                                          False,
                                          user=user,
                                          **machine)
Exemplo n.º 8
0
def poll_switch(poller_email,
                ip_addr,
                credentials,
                req_obj='mac',
                oper="SCAN"):
    """Query switch and update switch machines.

    .. note::
       When polling switch succeeds, for each mac it got from polling switch,
       A Machine record associated with the switch is added to the database.

    :param ip_addr: switch ip address.
    :type ip_addr: str
    :param credentials: switch crednetials.
    :type credentials: dict
    :param req_obj: the object requested to query from switch.
    :type req_obj: str
    :param oper: the operation to query the switch.
    :type oper: str, should be one of ['SCAN', 'GET', 'SET']

    .. note::
       The function should be called out of database session scope.
    """
    poller = user_api.get_user_object(poller_email)
    ip_int = long(netaddr.IPAddress(ip_addr))
    with util.lock('poll switch %s' % ip_addr, timeout=120) as lock:
        if not lock:
            raise Exception('failed to acquire lock to poll switch %s' %
                            ip_addr)

        # TODO(grace): before repoll the switch, set the state to repolling.
        # and when the poll switch is timeout, set the state to error.
        # the frontend should only consider some main state like INTIALIZED,
        # ERROR and SUCCESSFUL, REPOLLING is as an intermediate state to
        # indicate the switch is in learning the mac of the machines connected
        # to it.
        logging.debug('poll switch: %s', ip_addr)
        switch_dict, machine_dicts = _poll_switch(ip_addr,
                                                  credentials,
                                                  req_obj=req_obj,
                                                  oper=oper)
        switches = switch_api.list_switches(ip_int=ip_int, user=poller)
        if not switches:
            logging.error('no switch found for %s', ip_addr)
            return

        for switch in switches:
            for machine_dict in machine_dicts:
                logging.info('add machine: %s', machine_dict)
                machine_dict['owner_id'] = poller.id
                switch_api.add_switch_machine(switch['id'],
                                              False,
                                              user=poller,
                                              **machine_dict)
                switch_api.update_switch(switch['id'],
                                         user=poller,
                                         **switch_dict)
Exemplo n.º 9
0
 def test_list_machines(self):
     switch.add_switch_machine(
         self.user_object,
         1,
         mac='28:6e:d4:46:c4:25',
         port='1'
     )
     list_machine = machine.list_machines(self.user_object)
     self.assertIsNotNone(list_machine)
Exemplo n.º 10
0
 def test_list_machines(self):
     switch.add_switch_machine(
         1,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     list_machine = machine.list_machines(self.user_object)
     self.assertIsNotNone(list_machine)
     self.assertEqual(list_machine[0]['mac'], '28:6e:d4:46:c4:25')
Exemplo n.º 11
0
 def test_list_machines(self):
     switch.add_switch_machine(
         1,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     list_machine = machine.list_machines(self.user_object)
     self.assertIsNotNone(list_machine)
     self.assertEqual(list_machine[0]['mac'], '28:6e:d4:46:c4:25')
Exemplo n.º 12
0
def poll_switch(poller_email, ip_addr, credentials,
                req_obj='mac', oper="SCAN"):
    """Query switch and update switch machines.

    .. note::
       When polling switch succeeds, for each mac it got from polling switch,
       A Machine record associated with the switch is added to the database.

    :param ip_addr: switch ip address.
    :type ip_addr: str
    :param credentials: switch crednetials.
    :type credentials: dict
    :param req_obj: the object requested to query from switch.
    :type req_obj: str
    :param oper: the operation to query the switch.
    :type oper: str, should be one of ['SCAN', 'GET', 'SET']

    .. note::
       The function should be called out of database session scope.
    """
    poller = user_api.get_user_object(poller_email)
    ip_int = long(netaddr.IPAddress(ip_addr))
    with util.lock('poll switch %s' % ip_addr, timeout=120) as lock:
        if not lock:
            raise Exception(
                'failed to acquire lock to poll switch %s' % ip_addr
            )

        # TODO(grace): before repoll the switch, set the state to repolling.
        # and when the poll switch is timeout, set the state to error.
        # the frontend should only consider some main state like INTIALIZED,
        # ERROR and SUCCESSFUL, REPOLLING is as an intermediate state to
        # indicate the switch is in learning the mac of the machines connected
        # to it.
        logging.debug('poll switch: %s', ip_addr)
        switch_dict, machine_dicts = _poll_switch(
            ip_addr, credentials, req_obj=req_obj, oper=oper
        )
        switches = switch_api.list_switches(ip_int=ip_int, user=poller)
        if not switches:
            logging.error('no switch found for %s', ip_addr)
            return

        for switch in switches:
            for machine_dict in machine_dicts:
                logging.debug('add machine: %s', machine_dict)
                switch_api.add_switch_machine(
                    switch['id'], False, user=poller, **machine_dict
                )
                switch_api.update_switch(
                    switch['id'],
                    user=poller,
                    **switch_dict
                )
Exemplo n.º 13
0
 def test_get_machine(self):
     switch.add_switch_machine(
         self.user_object,
         1,
         mac='28:6e:d4:46:c4:25',
         port='1'
     )
     get_machine = machine.get_machine(
         self.user_object,
         1
     )
     self.assertIsNotNone(get_machine)
Exemplo n.º 14
0
 def test_get_switchmachine(self):
     switch.add_switch_machine(
         1,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     get_switchmachine = switch.get_switchmachine(
         1,
         user=self.user_object,
     )
     self.assertIsNotNone(get_switchmachine)
     self.assertEqual(get_switchmachine['mac'], '28:6e:d4:46:c4:25')
Exemplo n.º 15
0
 def test_del_machine(self):
     switch.add_switch_machine(
         1,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     machine.del_machine(
         1,
         user=self.user_object,
     )
     del_machine = machine.list_machines(self.user_object)
     self.assertEqual([], del_machine)
Exemplo n.º 16
0
 def test_del_machine(self):
     switch.add_switch_machine(
         1,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     machine.del_machine(
         1,
         user=self.user_object,
     )
     del_machine = machine.list_machines(self.user_object)
     self.assertEqual([], del_machine)
Exemplo n.º 17
0
 def test_get_machine(self):
     switch.add_switch_machine(
         1,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     get_machine = machine.get_machine(
         1,
         user=self.user_object,
     )
     self.assertIsNotNone(get_machine)
     self.assertEqual(get_machine['mac'], '28:6e:d4:46:c4:25')
Exemplo n.º 18
0
 def test_patch_machine(self):
     switch.add_switch_machine(
         1,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     machine.patch_machine(1,
                           user=self.user_object,
                           tag={'patched_tag': 'test'})
     patch_machine = machine.list_machines(self.user_object)
     expected = {'tag': {'patched_tag': 'test'}}
     self.assertTrue(
         all(item in patch_machine[0].items() for item in expected.items()))
Exemplo n.º 19
0
 def test_reset_machine(self):
     switch.add_switch_machine(
         1,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     from compass.tasks import client as celery_client
     celery_client.celery.send_task = mock.Mock()
     reset_machine = machine.reset_machine(1, {'reset_machine': True},
                                           user=self.user_object)
     expected = {'status': 'reset 28:6e:d4:46:c4:25 action sent'}
     self.assertTrue(
         all(item in reset_machine.items() for item in expected.items()))
Exemplo n.º 20
0
 def test_list_hosts_without_ip(self):
     switch.add_switch(
         self.user_object,
         ip='2887583784'
     )
     switch.add_switch_machine(
         self.user_object,
         2,
         mac='28:6e:d4:46:c4:25',
         port='1'
     )
     list_hosts = switch.list_switchmachines_hosts(
         self.user_object
     )
     self.assertIsNotNone(list_hosts)
Exemplo n.º 21
0
 def test_switchmachine(self):
     switch.add_switch_machine(
         self.user_object,
         1,
         mac='28:6e:d4:46:c4:25',
         port='1'
     )
     switch.del_switchmachine(
         self.user_object,
         1
     )
     del_switchmachine = switch.list_switchmachines(
         self.user_object
     )
     self.assertEqual([], del_switchmachine)
Exemplo n.º 22
0
def poll_switch(poller_email, ip_addr, credentials,
                req_obj='mac', oper="SCAN"):
    """Query switch and update switch machines.

    .. note::
       When polling switch succeeds, for each mac it got from polling switch,
       A Machine record associated with the switch is added to the database.

    :param ip_addr: switch ip address.
    :type ip_addr: str
    :param credentials: switch crednetials.
    :type credentials: dict
    :param req_obj: the object requested to query from switch.
    :type req_obj: str
    :param oper: the operation to query the switch.
    :type oper: str, should be one of ['SCAN', 'GET', 'SET']

    .. note::
       The function should be called out of database session scope.
    """
    poller = user_api.get_user_object(poller_email)
    ip_int = long(netaddr.IPAddress(ip_addr))
    with util.lock('poll switch %s' % ip_addr, timeout=120) as lock:
        if not lock:
            raise Exception(
                'failed to acquire lock to poll switch %s' % ip_addr
            )

        logging.debug('poll switch: %s', ip_addr)
        switch_dict, machine_dicts = _poll_switch(
            ip_addr, credentials, req_obj=req_obj, oper=oper
        )
        switches = switch_api.list_switches(ip_int=ip_int, user=poller)
        if not switches:
            logging.error('no switch found for %s', ip_addr)
            return

        for switch in switches:
            for machine_dict in machine_dicts:
                logging.debug('add machine: %s', machine_dict)
                switch_api.add_switch_machine(
                    switch['id'], False, user=poller, **machine_dict
                )
                switch_api.update_switch(
                    switch['id'],
                    user=poller,
                    **switch_dict
                )
Exemplo n.º 23
0
 def test_patch_switchmachine(self):
     switch.add_switch_machine(
         1,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     switch.patch_switchmachine(
         1,
         user=self.user_object,
         location={'patched_location': 'test_location'})
     patch_switchmachine = switch.list_switchmachines(user=self.user_object)
     expected = {'location': {'patched_location': 'test_location'}}
     self.assertTrue(
         all(item in patch_switchmachine[0].items()
             for item in expected.items()))
Exemplo n.º 24
0
 def test_list_switch_machines_ip_invalid(self):
     switch.add_switch(
         self.user_object,
         ip='2887583784'
     )
     switch.add_switch_machine(
         self.user_object,
         2,
         mac='28:6e:d4:46:c4:25',
         port='1'
     )
     list_switch_machines = switch.list_switchmachines(
         self.user_object,
         switch_ip_int='test'
     )
     self.assertEqual(list_switch_machines, [])
Exemplo n.º 25
0
 def test_list_hosts_ip_invalid(self):
     switch.add_switch(
         ip='2887583784',
         user=self.user_object,
     )
     switch.add_switch_machine(
         2,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     list_hosts = switch.list_switchmachines_hosts(
         switch_ip_int='test',
         user=self.user_object,
     )
     self.assertEqual(list_hosts, [])
Exemplo n.º 26
0
 def test_list_hosts_ip_invalid(self):
     switch.add_switch(
         ip='2887583784',
         user=self.user_object,
     )
     switch.add_switch_machine(
         2,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     list_hosts = switch.list_switchmachines_hosts(
         switch_ip_int='test',
         user=self.user_object,
     )
     self.assertEqual(list_hosts, [])
Exemplo n.º 27
0
 def test_list_hosts_without_ip(self):
     switch.add_switch(
         ip='2887583784',
         user=self.user_object,
     )
     switch.add_switch_machine(
         2,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     list_hosts = switch.list_switchmachines_hosts(user=self.user_object)
     expected = {'switch_ip': '172.29.8.40'}
     self.assertTrue(
         all(item in list_hosts[0].items() for item in expected.items()))
     self.assertIsNotNone(list_hosts)
Exemplo n.º 28
0
def add_switch_machine():
    """add switch machine."""
    data = _get_request_data()
    return utils.make_json_response(
        200,
        switch_api.add_switch_machine(current_user, switch_id, **data)
    )
Exemplo n.º 29
0
 def test_get_switch_machine(self):
     switch.add_switch(
         self.user_object,
         ip='2887583784'
     )
     switch.add_switch_machine(
         self.user_object,
         2,
         mac='28:6e:d4:46:c4:25',
         port='1'
     )
     get_switch_machine = switch.get_switch_machine(
         self.user_object,
         2,
         1
     )
     self.assertIsNotNone(get_switch_machine)
Exemplo n.º 30
0
 def test_list_hosts_with_ip_int(self):
     switch.add_switch(
         self.user_object,
         ip='2887583784'
     )
     switch.add_switch_machine(
         self.user_object,
         2,
         mac='28:6e:d4:46:c4:25',
         port='1'
     )
     list_hosts = switch.list_switchmachines_hosts(
         self.user_object,
         switch_ip_int='2887583784'
     )
     expected = '172.29.8.40'
     self.assertTrue(expected for item in list_hosts[0].items())
Exemplo n.º 31
0
 def test_update_machine(self):
     switch.add_switch_machine(
         self.user_object,
         1,
         mac='28:6e:d4:46:c4:25',
         port='1'
     )
     machine.update_machine(
         self.user_object,
         1,
         tag='test'
     )
     update_machine = machine.list_machines(self.user_object)
     expected = {'tag': 'test'}
     self.assertTrue(
         item in update_machine[0].items() for item in expected.items()
     )
Exemplo n.º 32
0
 def test_get_switch_machine(self):
     switch.add_switch(
         ip='2887583784',
         user=self.user_object,
     )
     switch.add_switch_machine(
         2,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     get_switch_machine = switch.get_switch_machine(
         2,
         1,
         user=self.user_object,
     )
     self.assertIsNotNone(get_switch_machine)
     self.assertEqual(get_switch_machine['mac'], '28:6e:d4:46:c4:25')
Exemplo n.º 33
0
 def test_add_switch_machine(self):
     add_switch_machine = switch.add_switch_machine(
         1,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     expected = '28:6e:d4:46:c4:25'
     self.assertEqual(expected, add_switch_machine['mac'])
Exemplo n.º 34
0
 def test_add_switch_machine(self):
     add_switch_machine = switch.add_switch_machine(
         1,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     expected = '28:6e:d4:46:c4:25'
     self.assertEqual(expected, add_switch_machine['mac'])
Exemplo n.º 35
0
 def test_list_switch_machines_without_ip(self):
     switch.add_switch(
         ip='2887583784',
         user=self.user_object,
     )
     switch.add_switch_machine(
         2,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     list_switch_machines = switch.list_switchmachines(
         user=self.user_object
     )
     expected = {'switch_ip': '172.29.8.40'}
     self.assertTrue(
         all(item in list_switch_machines[0].items()
             for item in expected.items()))
Exemplo n.º 36
0
 def test_add_switch_machine_session(self):
     with database.session() as session:
         add_switch_machine = switch.add_switch_machine(
             1,
             mac='28:6e:d4:46:c4:25',
             user=self.user_object,
             session=session,
             port='1')
     expected = '28:6e:d4:46:c4:25'
     self.assertEqual(expected, add_switch_machine['mac'])
Exemplo n.º 37
0
 def test_list_switch_machines_with_ip_int(self):
     switch.add_switch(
         ip='2887583784',
         user=self.user_object,
     )
     switch.add_switch_machine(
         2,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     list_switch_machines = switch.list_switchmachines(
         switch_ip_int='2887583784',
         user=self.user_object,
     )
     expected = {'switch_ip': '172.29.8.40'}
     self.assertTrue(
         all(item in list_switch_machines[0].items()
             for item in expected.items()))
Exemplo n.º 38
0
 def test_reset_machine(self):
     switch.add_switch_machine(
         1,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     from compass.tasks import client as celery_client
     celery_client.celery.send_task = mock.Mock()
     reset_machine = machine.reset_machine(
         1,
         {'reset_machine': True},
         user=self.user_object
     )
     expected = {
         'status': 'reset 28:6e:d4:46:c4:25 action sent'
     }
     self.assertTrue(all(
         item in reset_machine.items() for item in expected.items())
     )
Exemplo n.º 39
0
 def test_update_switchmachine(self):
     switch.add_switch_machine(
         self.user_object,
         1,
         mac='28:6e:d4:46:c4:25',
         port='1'
     )
     switch.update_switchmachine(
         self.user_object,
         1,
         location='test_location'
     )
     update_switchmachine = switch.list_switchmachines(
         self.user_object,
     )
     expected = {'location': 'test_location'}
     self.assertTrue(
         item in update_switchmachine[0].items()
         for item in expected.items()
     )
Exemplo n.º 40
0
 def test_add_switch_machine_session(self):
     with database.session() as session:
         add_switch_machine = switch.add_switch_machine(
             1,
             mac='28:6e:d4:46:c4:25',
             user=self.user_object,
             session=session,
             port='1'
         )
     expected = '28:6e:d4:46:c4:25'
     self.assertEqual(expected, add_switch_machine['mac'])
Exemplo n.º 41
0
 def test_patch_switchmachine(self):
     switch.add_switch_machine(
         self.user_object,
         1,
         mac='28:6e:d4:46:c4:25',
         port='1'
     )
     switch.patch_switchmachine(
         self.user_object,
         1,
         patched_location={
             'patched_location': 'test_location'
         }
     )
     patch_switchmachine = switch.list_switchmachines(
         self.user_object
     )
     expected = {'patched_location': 'test_location'}
     self.assertTrue(
         item in patch_switchmachine[0].items() for item in expected.items()
     )
Exemplo n.º 42
0
 def test_update_switch_machines_remove(self):
     switch.add_switch(
         ip='2887583784',
         user=self.user_object,
     )
     switch.add_switch_machine(
         2,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     switch.update_switch_machines(
         2,
         remove_machines=1,
         user=self.user_object,
     )
     update_remove = switch.list_switch_machines(
         2,
         user=self.user_object,
     )
     self.assertEqual([], update_remove)
Exemplo n.º 43
0
 def test_update_switch_machines_remove(self):
     switch.add_switch(
         ip='2887583784',
         user=self.user_object,
     )
     switch.add_switch_machine(
         2,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     switch.update_switch_machines(
         2,
         remove_machines=1,
         user=self.user_object,
     )
     update_remove = switch.list_switch_machines(
         2,
         user=self.user_object,
     )
     self.assertEqual([], update_remove)
Exemplo n.º 44
0
 def test_update_machine(self):
     switch.add_switch_machine(
         1,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     machine.update_machine(
         1,
         tag='test',
         user=self.user_object,
     )
     update_machine = machine.list_machines(self.user_object)
     expected = {
         'id': 1,
         'mac': '28:6e:d4:46:c4:25',
         'tag': 'test',
         'switch_ip': '0.0.0.0',
         'port': '1'
     }
     self.assertTrue(
         all(item in update_machine[0].items() for item in expected.items())
     )
Exemplo n.º 45
0
 def test_update_machine(self):
     switch.add_switch_machine(
         1,
         mac='28:6e:d4:46:c4:25',
         port='1',
         user=self.user_object,
     )
     machine.update_machine(
         1,
         tag='test',
         user=self.user_object,
     )
     update_machine = machine.list_machines(self.user_object)
     expected = {
         'id': 1,
         'mac': '28:6e:d4:46:c4:25',
         'tag': 'test',
         'switch_ip': '0.0.0.0',
         'port': '1'
     }
     self.assertTrue(
         all(item in update_machine[0].items()
             for item in expected.items()))
Exemplo n.º 46
0
 def test_pathc_switch_machine(self):
     switch.add_switch_machine(
         self.user_object,
         1,
         mac='28:6e:d4:46:c4:25',
         port='1'
     )
     switch.patch_switch_machine(
         self.user_object,
         1,
         1,
         patched_tag={
             'patched_tag': 'test_patched_tag'
         }
     )
     switch_patch_switch_machine = switch.list_switch_machines(
         self.user_object,
         1
     )
     expected = {'patched_tag': 'test_patched_tag'}
     self.assertTrue(
         item in switch_patch_switch_machine[0].items()
         for item in expected.items()
     )
Exemplo n.º 47
0
    def _prepare_database(self):
        adapter.load_adapters()
        metadata.load_metadatas()

        self.user_object = (
            user_api.get_user_object(
                setting.COMPASS_ADMIN_EMAIL
            )
        )
        self.adapter_id = None
        self.os_id = None
        self.flavor_id = None
        self.cluster_id = None

        # get adapter information
        list_adapters = adapter.list_adapters(user=self.user_object)
        for adptr in list_adapters:
            self.adapter_id = None
            if adptr['name'] != ADAPTER_NAME:
                continue
            self.adapter_id = adptr['id']
            self.os_id = None
            for supported_os in adptr['supported_oses']:
                if supported_os['name'] == OS_NAME:
                    self.os_id = supported_os['os_id']
                    break
            if not self.os_id:
                continue
            if (
                'package_installer' in adptr.keys() and
                adptr['flavors'] != [] and
                adptr['distributed_system_name'] == 'openstack'
            ):
                self.flavor_id = None
                for flavor in adptr['flavors']:
                    if flavor['name'] == 'allinone':
                        self.flavor_id = flavor['id']
                        break
                if not self.flavor_id:
                    continue
            else:
                continue
            if self.adapter_id and self.os_id and self.flavor_id:
                break

        if not self.adapter_id:
            raise Exception('adapter id not found')
        if not self.os_id:
            raise Exception('os id not found')
        if not self.flavor_id:
            raise Exception('flavor id not found')

        # add cluster
        cluster.add_cluster(
            adapter_id=self.adapter_id,
            os_id=self.os_id,
            flavor_id=self.flavor_id,
            name='test_cluster',
            user=self.user_object,
        )
        list_clusters = cluster.list_clusters(user=self.user_object)
        for list_cluster in list_clusters:
            if list_cluster['name'] == 'test_cluster':
                self.cluster_id = list_cluster['id']
                break
        for list_cluster in list_clusters:
            self.cluster_id = list_cluster['id']

        # add switch
        switch.add_switch(
            ip=SWITCH_IP,
            user=self.user_object,
        )
        list_switches = switch.list_switches(user=self.user_object)
        for list_switch in list_switches:
            self.switch_id = list_switch['id']
        switch.add_switch_machine(
            self.switch_id,
            user=self.user_object,
            mac=MACHINE_MAC,
            port='1'
        )

        # get machine information
        list_machines = machine.list_machines(user=self.user_object)
        for list_machine in list_machines:
            self.machine_id = list_machine['id']

        # add cluster host
        cluster.add_cluster_host(
            self.cluster_id,
            user=self.user_object,
            machine_id=self.machine_id,
            name='test_clusterhost'
        )
        list_clusterhosts = cluster.list_clusterhosts(user=self.user_object)
        for list_clusterhost in list_clusterhosts:
            self.host_id = list_clusterhost['host_id']
            self.clusterhost_id = list_clusterhost['clusterhost_id']

        # add subnet
        network.add_subnet(
            subnet=SUBNET,
            user=self.user_object,
        )
        list_subnets = network.list_subnets(
            user=self.user_object
        )
        for list_subnet in list_subnets:
            self.subnet_id = list_subnet['id']

        # add host network
        host.add_host_network(
            self.host_id,
            user=self.user_object,
            interface='eth0',
            ip=HOST_IP,
            subnet_id=self.subnet_id,
            is_mgmt=True
        )

        # get clusterhost
        list_clusterhosts = cluster.list_clusterhosts(
            user=self.user_object
        )
        for list_clusterhost in list_clusterhosts:
            self.clusterhost_id = list_clusterhost['id']

        # update host state
        self.list_hosts = host.list_hosts(user=self.user_object)
        for list_host in self.list_hosts:
            self.host_id = list_host['id']
        self.host_state = host.update_host_state(
            self.host_id,
            user=self.user_object,
            state='INSTALLING'
        )

        # update cluster state
        cluster.update_cluster_state(
            self.cluster_id,
            user=self.user_object,
            state='INSTALLING'
        )

        # update clusterhost state
        cluster.update_clusterhost_state(
            self.clusterhost_id,
            user=self.user_object,
            state='INSTALLING'
        )
Exemplo n.º 48
0
    def setUp(self):
        super(HostTestCase, self).setUp()
        os.environ['COMPASS_IGNORE_SETTING'] = 'true'
        os.environ['COMPASS_CONFIG_DIR'] = os.path.join(
            os.path.dirname(os.path.abspath(__file__)),
            'data'
        )
        reload(setting)
        database.init('sqlite://')
        database.create_db()
        adapter.load_adapters(force_reload=True)
        metadata.load_metadatas(force_reload=True)
        adapter.load_flavors(force_reload=True)

        self.user_object = (
            user_api.get_user_object(
                setting.COMPASS_ADMIN_EMAIL
            )
        )
        # get adapter information
        list_adapters = adapter.list_adapters(user=self.user_object)
        for list_adapter in list_adapters:
            for supported_os in list_adapter['supported_oses']:
                self.os_id = supported_os['os_id']
                break
            if list_adapter['flavors']:
                details = list_adapter['flavors']
                for detail in details:
                    if detail['display_name'] == 'allinone':
                        roles = detail['roles']
                        for role in roles:
                            self.adapter_id = role['adapter_id']
                            self.flavor_id = role['flavor_id']
                            break

        # add cluster
        cluster_names = ['test_cluster1', 'test_cluster2']
        for cluster_name in cluster_names:
            cluster.add_cluster(
                user=self.user_object,
                adapter_id=self.adapter_id,
                os_id=self.os_id,
                flavor_id=self.flavor_id,
                name=cluster_name
            )
        clusters = cluster.list_clusters(user=self.user_object)
        self.roles = None
        for list_cluster in clusters:
            for item in list_cluster['flavor']['roles']:
                self.roles = item
            if list_cluster['name'] == 'test_cluster1':
                self.cluster_id = list_cluster['id']
                break
        # add switch
        switch.add_switch(
            user=self.user_object,
            ip='172.29.8.40'
        )
        switches = switch.list_switches(user=self.user_object)
        self.switch_id = None
        for item in switches:
            self.switch_id = item['id']
        macs = ['28:6e:d4:46:c4:25', '00:0c:29:bf:eb:1d']
        for mac in macs:
            switch.add_switch_machine(
                self.switch_id,
                user=self.user_object,
                mac=mac,
                port='1'
            )
        # get machine information
        machines = machine.list_machines(user=self.user_object)
        self.machine_ids = []
        for item in machines:
            self.machine_ids.append(item['id'])
        # add cluster host
        name = ['newname1', 'newname2']
        for i in range(0, 2):
            cluster.add_cluster_host(
                self.cluster_id,
                user=self.user_object,
                machine_id=self.machine_ids[i],
                name=name[i]
            )
        self.host_ids = []
        clusterhosts = cluster.list_clusterhosts(user=self.user_object)
        for clusterhost in clusterhosts:
            self.host_ids.append(clusterhost['host_id'])
        # add subnet
        subnets = ['10.145.88.0/23', '192.168.100.0/23']
        for subnet in subnets:
            network.add_subnet(
                user=self.user_object,
                subnet=subnet
            )
        list_subnet = network.list_subnets(
            user=self.user_object
        )
        self.subnet_ids = []
        for item in list_subnet:
            self.subnet_ids.append(item['id'])
        # add host network
        host.add_host_network(
            self.host_ids[0],
            user=self.user_object,
            interface='eth0',
            ip='10.145.88.0',
            subnet_id=self.subnet_ids[0],
            is_mgmt=True
        )
        host.add_host_network(
            self.host_ids[1],
            user=self.user_object,
            interface='eth1',
            ip='192.168.100.0',
            subnet_id=self.subnet_ids[1],
            is_promiscuous=True
        )
        # add log history
        filenames = ['log1', 'log2']
        for filename in filenames:
            host.add_host_log_history(
                self.host_ids[0],
                user=self.user_object,
                filename=filename
            )

        self.os_configs = {
            'general': {
                'language': 'EN',
                'timezone': 'UTC',
                'http_proxy': 'http://127.0.0.1:3128',
                'https_proxy': 'http://127.0.0.1:3128',
                'no_proxy': [
                    '127.0.0.1',
                    'compass'
                ],
                'ntp_server': '127.0.0.1',
                'dns_servers': [
                    '127.0.0.1'
                ],
                'domain': 'ods.com',
                'search_path': [
                    'ods.com'
                ],
                'default_gateway': '127.0.0.1',
            },
            'server_credentials': {
                'username': '******',
                'password': '******',
            },
            'partition': {
                '/var': {
                    'max_size': '100G',
                    'percentage': 10,
                    'size': '1G'
                }
            }
        }
        self.package_configs = {
            'security': {
                'service_credentials': {
                    '$service': {
                        'username': '******',
                        'password': '******'
                    }
                },
                'console_credentials': {
                    '$console': {
                        'username': '******',
                        'password': '******'
                    }
                }
            },
            'network_mapping': {
                '$interface_type': 'eth0'
            }
        }