Beispiel #1
0
    def test_task_manager_task_parallel_process(self):
        tasks = []

        def _exec(task):
            task.userdata['executed'] = True
            return TaskStatus.PENDING

        def _status(task):
            for t in tasks:
                if not t.userdata.get('executed'):
                    t.userdata['resut'] = False
            return TaskStatus.COMPLETED

        def _result(task):
            if (task.userdata.get('result') is None and
                task.status == TaskStatus.COMPLETED):
                task.userdata['result'] = True
            else:
                task.userdata['result'] = False

        for i in range(5):
            name = "name-%d" % i
            res = 'resource-%d' % i
            task = ts.Task(name, res, _exec, _status, _result, {})
            tasks.append(task)
            self.manager.add(task)

        for task in tasks:
            task.wait(TaskState.RESULT)
            self.assertTrue(task.userdata['result'])
Beispiel #2
0
 def create_dnat_rule(self,
                      router_id,
                      edge_id,
                      dst,
                      translated,
                      jobdata=None,
                      location=None):
     # TODO(fank): use POST for optimization
     #             return rule_id for future reference
     LOG.debug(_("VCNS: create dnat rule %(dst)s/%(translated)s"), {
         'dst': dst,
         'translated': translated
     })
     dnat_rule = self._assemble_nat_rule("dnat", dst, translated)
     userdata = {
         'router_id': router_id,
         'edge_id': edge_id,
         'rule': dnat_rule,
         'location': location,
         'jobdata': jobdata
     }
     task_name = "create-dnat-%s-%s-%s" % (edge_id, dst, translated)
     task = tasks.Task(task_name,
                       router_id,
                       self._create_nat_rule,
                       userdata=userdata)
     task.add_result_monitor(self.callbacks.dnat_create_result)
     self.task_manager.add(task)
     return task
Beispiel #3
0
    def update_routes(self,
                      router_id,
                      edge_id,
                      gateway,
                      routes,
                      skippable=True,
                      jobdata=None):
        if gateway:
            gateway = gateway.split('/')[0]

        userdata = {
            'edge_id': edge_id,
            'gateway': gateway,
            'routes': routes,
            'skippable': skippable,
            'jobdata': jobdata
        }
        task_name = "update-routes-%s" % (edge_id)
        task = tasks.Task(task_name,
                          router_id,
                          self._update_routes,
                          userdata=userdata)
        task.add_result_monitor(self.callbacks.routes_update_result)
        self.updated_task['route'][edge_id] = task
        self.task_manager.add(task)
        return task
Beispiel #4
0
 def create_snat_rule(self,
                      router_id,
                      edge_id,
                      src,
                      translated,
                      jobdata=None,
                      location=None):
     LOG.debug(_("VCNS: create snat rule %(src)s/%(translated)s"), {
         'src': src,
         'translated': translated
     })
     snat_rule = self._assemble_nat_rule("snat", src, translated)
     userdata = {
         'router_id': router_id,
         'edge_id': edge_id,
         'rule': snat_rule,
         'location': location,
         'jobdata': jobdata
     }
     task_name = "create-snat-%s-%s-%s" % (edge_id, src, translated)
     task = tasks.Task(task_name,
                       router_id,
                       self._create_nat_rule,
                       userdata=userdata)
     task.add_result_monitor(self.callbacks.snat_create_result)
     self.task_manager.add(task)
     return task
Beispiel #5
0
    def test_task_manager_stop(self):
        def _exec(task):
            return TaskStatus.PENDING

        def _status(task):
            greenthread.sleep(0.1)
            return TaskStatus.PENDING

        def _result(task):
            pass

        manager = ts.TaskManager().start(100)

        alltasks = {}
        for i in range(100):
            res = 'res-%d' % i
            tasks = []
            for i in range(100):
                task = ts.Task('name', res, _exec, _status, _result)
                manager.add(task)
                tasks.append(task)
            alltasks[res] = tasks

        greenthread.sleep(2)
        manager.stop()
        for res, tasks in alltasks.iteritems():
            for task in tasks:
                self.assertEqual(task.status, TaskStatus.ABORT)
    def deploy_edge(self,
                    router_id,
                    name,
                    internal_network,
                    jobdata=None,
                    wait_for_exec=False,
                    loadbalancer_enable=True):
        task_name = 'deploying-%s' % name
        edge_name = name
        edge = self._assemble_edge(
            edge_name,
            datacenter_moid=self.datacenter_moid,
            deployment_container_id=self.deployment_container_id,
            appliance_size='large',
            remote_access=True)
        appliance = self._assemble_edge_appliance(self.resource_pool_id,
                                                  self.datastore_id)
        if appliance:
            edge['appliances']['appliances'] = [appliance]

        vnic_external = self._assemble_edge_vnic(
            vcns_const.EXTERNAL_VNIC_NAME,
            vcns_const.EXTERNAL_VNIC_INDEX,
            self.external_network,
            type="uplink")
        edge['vnics']['vnics'].append(vnic_external)
        vnic_inside = self._assemble_edge_vnic(
            vcns_const.INTERNAL_VNIC_NAME,
            vcns_const.INTERNAL_VNIC_INDEX,
            internal_network,
            vcns_const.INTEGRATION_EDGE_IPADDRESS,
            vcns_const.INTEGRATION_SUBNET_NETMASK,
            type="internal")
        edge['vnics']['vnics'].append(vnic_inside)
        if loadbalancer_enable:
            self._enable_loadbalancer(edge)
        userdata = {'request': edge, 'router_name': name, 'jobdata': jobdata}
        task = tasks.Task(task_name,
                          router_id,
                          self._deploy_edge,
                          status_callback=self._status_edge,
                          result_callback=self._result_edge,
                          userdata=userdata)
        task.add_executed_monitor(self.callbacks.edge_deploy_started)
        task.add_result_monitor(self.callbacks.edge_deploy_result)
        self.task_manager.add(task)

        if wait_for_exec:
            # waitl until the deploy task is executed so edge_id is available
            task.wait(TaskState.EXECUTED)

        return task
Beispiel #7
0
 def delete_edge(self, router_id, edge_id, jobdata=None):
     task_name = 'delete-%s' % edge_id
     userdata = {
         'router_id': router_id,
         'edge_id': edge_id,
         'jobdata': jobdata
     }
     task = tasks.Task(task_name,
                       router_id,
                       self._delete_edge,
                       userdata=userdata)
     task.add_result_monitor(self.callbacks.edge_delete_result)
     self.task_manager.add(task)
     return task
Beispiel #8
0
 def delete_snat_rule(self, router_id, edge_id, src, jobdata=None):
     LOG.debug(_("VCNS: delete snat rule %s"), src)
     userdata = {
         'edge_id': edge_id,
         'address': src,
         'addrtype': 'originalAddress',
         'jobdata': jobdata
     }
     task_name = "delete-snat-%s-%s" % (edge_id, src)
     task = tasks.Task(task_name,
                       router_id,
                       self._delete_nat_rule,
                       userdata=userdata)
     task.add_result_monitor(self.callbacks.snat_delete_result)
     self.task_manager.add(task)
     return task
Beispiel #9
0
 def delete_dnat_rule(self, router_id, edge_id, translated, jobdata=None):
     # TODO(fank): pass in rule_id for optimization
     LOG.debug(_("VCNS: delete dnat rule %s"), translated)
     userdata = {
         'edge_id': edge_id,
         'address': translated,
         'addrtype': 'translatedAddress',
         'jobdata': jobdata
     }
     task_name = "delete-dnat-%s-%s" % (edge_id, translated)
     task = tasks.Task(task_name,
                       router_id,
                       self._delete_nat_rule,
                       userdata=userdata)
     task.add_result_monitor(self.callbacks.dnat_delete_result)
     self.task_manager.add(task)
     return task
Beispiel #10
0
    def test_task_manager_task_ordered_process(self):
        def _task_failed(task, reason):
            task.userdata['result'] = False
            task.userdata['error'] = reason

        def _exec(task):
            task.userdata['executed'] = True
            return TaskStatus.PENDING

        def _status(task):
            return TaskStatus.COMPLETED

        def _result(task):
            next_task = task.userdata.get('next')
            if next_task:
                if next_task.userdata.get('executed'):
                    _task_failed(next_task, "executed premature")
            if task.userdata.get('result', True):
                task.userdata['result'] = True

        tasks = []
        prev = None
        last_task = None
        for i in range(5):
            name = "name-%d" % i
            task = ts.Task(name, 'res', _exec, _status, _result, {})
            tasks.append(task)
            if prev:
                prev.userdata['next'] = task
            prev = task
            last_task = task

        for task in tasks:
            self.manager.add(task)

        last_task.wait(TaskState.RESULT)

        for task in tasks:
            if 'error' in task.userdata:
                print "Task %s failed: " % (tasks.name,
                                            tasks.userdata['error'])

        for task in tasks:
            self.assertTrue(task.userdata['result'])
Beispiel #11
0
    def _test_task_manager_stop(self,
                                exec_wait=False,
                                result_wait=False,
                                stop_wait=0):
        def _exec(task):
            if exec_wait:
                greenthread.sleep(0.01)
            return TaskStatus.PENDING

        def _status(task):
            greenthread.sleep(0.01)
            return TaskStatus.PENDING

        def _result(task):
            if result_wait:
                greenthread.sleep(0)
            pass

        manager = ts.TaskManager().start(100)
        manager.stop()
        # Task manager should not leave running threads around
        # if _thread is None it means it was killed in stop()
        self.assertIsNone(manager._thread)
        manager.start(100)

        alltasks = {}
        for i in range(100):
            res = 'res-%d' % i
            tasks = []
            for i in range(100):
                task = ts.Task('name', res, _exec, _status, _result)
                manager.add(task)
                tasks.append(task)
            alltasks[res] = tasks

        greenthread.sleep(stop_wait)
        manager.stop()
        # Task manager should not leave running threads around
        # if _thread is None it means it was killed in stop()
        self.assertIsNone(manager._thread)

        for res, tasks in alltasks.iteritems():
            for task in tasks:
                self.assertEqual(task.status, TaskStatus.ABORT)
Beispiel #12
0
    def update_interface(self,
                         router_id,
                         edge_id,
                         index,
                         network,
                         address=None,
                         netmask=None,
                         secondary=None,
                         jobdata=None):
        LOG.debug(_("VCNS: update vnic %(index)d: %(addr)s %(netmask)s"), {
            'index': index,
            'addr': address,
            'netmask': netmask
        })
        if index == vcns_const.EXTERNAL_VNIC_INDEX:
            name = vcns_const.EXTERNAL_VNIC_NAME
            intf_type = 'uplink'
        elif index == vcns_const.INTERNAL_VNIC_INDEX:
            name = vcns_const.INTERNAL_VNIC_NAME
            intf_type = 'internal'
        else:
            msg = _("Vnic %d currently not supported") % index
            raise exceptions.VcnsGeneralException(msg)

        config = self._assemble_edge_vnic(name,
                                          index,
                                          network,
                                          address,
                                          netmask,
                                          secondary,
                                          type=intf_type)

        userdata = {'edge_id': edge_id, 'config': config, 'jobdata': jobdata}
        task_name = "update-interface-%s-%d" % (edge_id, index)
        task = tasks.Task(task_name,
                          router_id,
                          self._update_interface,
                          userdata=userdata)
        task.add_result_monitor(self.callbacks.interface_update_result)
        self.task_manager.add(task)
        return task
Beispiel #13
0
    def test_task_pending_task(self):
        def _exec(task):
            task.userdata['executing'] = True
            while not task.userdata['tested']:
                greenthread.sleep(0)
            task.userdata['executing'] = False
            return TaskStatus.COMPLETED

        userdata = {'executing': False, 'tested': False}
        manager = ts.TaskManager().start(100)
        task = ts.Task('name', 'res', _exec, userdata=userdata)
        manager.add(task)

        while not userdata['executing']:
            greenthread.sleep(0)
        self.assertTrue(manager.has_pending_task())

        userdata['tested'] = True
        while userdata['executing']:
            greenthread.sleep(0)
        self.assertFalse(manager.has_pending_task())
Beispiel #14
0
    def update_nat_rules(self, router_id, edge_id, snats, dnats, jobdata=None):
        LOG.debug(
            _("VCNS: update nat rule\n"
              "SNAT:%(snat)s\n"
              "DNAT:%(dnat)s\n"), {
                  'snat': snats,
                  'dnat': dnats
              })
        nat_rules = []

        for dnat in dnats:
            nat_rules.append(
                self._assemble_nat_rule('dnat', dnat['dst'],
                                        dnat['translated']))
            nat_rules.append(
                self._assemble_nat_rule('snat', dnat['translated'],
                                        dnat['dst']))

        for snat in snats:
            nat_rules.append(
                self._assemble_nat_rule('snat', snat['src'],
                                        snat['translated']))

        userdata = {
            'edge_id': edge_id,
            'rules': nat_rules,
            'jobdata': jobdata,
        }
        task_name = "update-nat-%s" % edge_id
        task = tasks.Task(task_name,
                          router_id,
                          self._update_nat_rule,
                          userdata=userdata)
        task.add_result_monitor(self.callbacks.nat_update_result)
        self.updated_task['nat'][edge_id] = task
        self.task_manager.add(task)
        return task
Beispiel #15
0
    def _test_task_manager_task_process_state(self, sync_exec=False):
        def _task_failed(task, reason):
            task.userdata['result'] = False
            task.userdata['error'] = reason

        def _check_state(task, exp_state):
            if not task.userdata.get('result', True):
                return False

            state = task.userdata['state']
            if state != exp_state:
                msg = "state %d expect %d" % (
                    state, exp_state)
                _task_failed(task, msg)
                return False

            task.userdata['state'] = state + 1
            return True

        def _exec(task):
            if not _check_state(task, 1):
                return TaskStatus.ERROR

            if task.userdata['sync_exec']:
                return TaskStatus.COMPLETED
            else:
                return TaskStatus.PENDING

        def _status(task):
            if task.userdata['sync_exec']:
                _task_failed(task, "_status callback triggered")

            state = task.userdata['state']
            if state == 3:
                _check_state(task, 3)
                return TaskStatus.PENDING
            else:
                _check_state(task, 4)
                return TaskStatus.COMPLETED

        def _result(task):
            if task.userdata['sync_exec']:
                exp_state = 3
            else:
                exp_state = 5

            _check_state(task, exp_state)

        def _start_monitor(task):
            _check_state(task, 0)

        def _executed_monitor(task):
            _check_state(task, 2)

        def _result_monitor(task):
            if task.userdata['sync_exec']:
                exp_state = 4
            else:
                exp_state = 6

            if _check_state(task, exp_state):
                task.userdata['result'] = True
            else:
                task.userdata['result'] = False

        userdata = {
            'state': 0,
            'sync_exec': sync_exec
        }
        task = ts.Task('name', 'res', _exec, _status, _result, userdata)
        task.add_start_monitor(_start_monitor)
        task.add_executed_monitor(_executed_monitor)
        task.add_result_monitor(_result_monitor)

        self.manager.add(task)

        task.wait(TaskState.RESULT)

        self.assertTrue(userdata['result'])