Ejemplo n.º 1
0
 def test_control_hooks(self):
     call_request = CallRequest(function)
     for key in dispatch_constants.CALL_CONTROL_HOOKS:
         self.assertTrue(call_request.control_hooks[key] is None)
     for key in dispatch_constants.CALL_CONTROL_HOOKS:
         call_request.add_control_hook(key, function)
         self.assertTrue(call_request.control_hooks[key] is function)
Ejemplo n.º 2
0
 def test_control_hooks(self):
     call_request = CallRequest(function)
     for key in dispatch_constants.CALL_CONTROL_HOOKS:
         self.assertTrue(call_request.control_hooks[key] is None)
     for key in dispatch_constants.CALL_CONTROL_HOOKS:
         call_request.add_control_hook(key, function)
         self.assertTrue(call_request.control_hooks[key] is function)
Ejemplo n.º 3
0
def consumer_content_update_itinerary(consumer_id, units, options):
    """
    Create an itinerary for consumer content update.
    @param consumer_id: unique id of the consumer
    @type consumer_id: str
    @param units: units to update
    @type units: list or tuple
    @param options: options to pass to the update manager
    @type options: dict or None
    @return: list of call requests
    @rtype: list
    """
    manager = managers_factory.consumer_agent_manager()
    args = [consumer_id]
    kwargs = {'units': units, 'options': options}
    weight = pulp_config.config.getint('tasks', 'consumer_content_weight')
    tags = [resource_tag(dispatch_constants.RESOURCE_CONSUMER_TYPE, consumer_id),
            action_tag('unit_update')]
    call_request = CallRequest(
        manager.update_content,
        args,
        kwargs,
        weight=weight,
        tags=tags,
        archive=True,
        asynchronous=True,
        kwarg_blacklist=['options'])
    call_request.add_control_hook(dispatch_constants.CALL_CANCEL_CONTROL_HOOK, cancel_agent_request)
    call_request.reads_resource(dispatch_constants.RESOURCE_CONSUMER_TYPE, consumer_id)
    return [call_request]
Ejemplo n.º 4
0
def consumer_content_update_itinerary(consumer_id, units, options):
    """
    Create an itinerary for consumer content update.
    @param consumer_id: unique id of the consumer
    @type consumer_id: str
    @param units: units to update
    @type units: list or tuple
    @param options: options to pass to the update manager
    @type options: dict or None
    @return: list of call requests
    @rtype: list
    """
    manager = managers_factory.consumer_agent_manager()
    args = [consumer_id]
    kwargs = {'units': units, 'options': options}
    weight = pulp_config.config.getint('tasks', 'consumer_content_weight')
    tags = [
        resource_tag(dispatch_constants.RESOURCE_CONSUMER_TYPE, consumer_id),
        action_tag('unit_update')
    ]
    call_request = CallRequest(manager.update_content,
                               args,
                               kwargs,
                               weight=weight,
                               tags=tags,
                               archive=True,
                               asynchronous=True)
    call_request.add_control_hook(dispatch_constants.CALL_CANCEL_CONTROL_HOOK,
                                  cancel_agent_request)
    call_request.reads_resource(dispatch_constants.RESOURCE_CONSUMER_TYPE,
                                consumer_id)
    return [call_request]
Ejemplo n.º 5
0
 def test_serialize_deserialize_with_control_hook(self):
     key = dispatch_constants.CALL_CANCEL_CONTROL_HOOK
     call_request = CallRequest(function)
     call_request.add_control_hook(key, function)
     data = call_request.serialize()
     self.assertTrue(isinstance(data, dict))
     call_request_2 = CallRequest.deserialize(data)
     self.assertTrue(isinstance(call_request_2, CallRequest))
     self.assertTrue(call_request_2.control_hooks[key] == function)
Ejemplo n.º 6
0
 def test_serialize_deserialize_with_control_hook(self):
     key = dispatch_constants.CALL_CANCEL_CONTROL_HOOK
     call_request = CallRequest(function)
     call_request.add_control_hook(key, function)
     data = call_request.serialize()
     self.assertTrue(isinstance(data, dict))
     call_request_2 = CallRequest.deserialize(data)
     self.assertTrue(isinstance(call_request_2, CallRequest))
     self.assertTrue(call_request_2.control_hooks[key] == function)
Ejemplo n.º 7
0
class TaskTests(base.PulpServerTests):

    def setUp(self):
        super(TaskTests, self).setUp()
        self.call_request = CallRequest(Call(), call_args, call_kwargs)
        self.call_report = CallReport()
        self.task = Task(self.call_request, self.call_report)

    def tearDown(self):
        super(TaskTests, self).tearDown()
        self.call_request = None
        self.call_report = None
        self.task = None

    def test_run(self):
        self.assertTrue(self.call_report.state is dispatch_constants.CALL_WAITING_STATE)
        self.task._run()
        self.assertTrue(self.call_request.call.call_count == 1)
        self.assertTrue(self.call_request.call.call_args[0] == call_args,
                        '%s != %s' % (str(self.call_request.call.call_args[0]), str(call_args)))
        self.assertTrue(self.call_request.call.call_args[1] == call_kwargs)
        self.assertTrue(self.call_report.state is dispatch_constants.CALL_FINISHED_STATE)

    def test_complete(self):
        now = datetime.datetime.now(dateutils.utc_tz())
        self.task._run()
        self.assertTrue(self.call_report.finish_time > now)

    def test_complete_callback(self):
        callback = mock.Mock()
        self.task.complete_callback = callback
        self.task._run()
        self.assertTrue(callback.call_count == 1)
        self.assertTrue(callback.call_args[0][0] is self.task)

    def test_cancel_control_hook(self):
        callback = mock.Mock()
        self.call_request.add_control_hook(dispatch_constants.CALL_CANCEL_CONTROL_HOOK, callback)
        self.call_report.state = dispatch_constants.CALL_RUNNING_STATE
        try:
            self.task.cancel()
        except:
            self.fail(traceback.format_exc())
        self.assertTrue(callback.call_count == 1)
        self.assertTrue(callback.call_args[0][0] is self.call_request)
        self.assertTrue(callback.call_args[0][1] is self.call_report)
        self.assertTrue(self.call_report.state is dispatch_constants.CALL_CANCELED_STATE,
                        self.call_report.state)

    def test_finish_execution_hook(self):
        hooks = [mock.Mock(), mock.Mock()]
        for h in hooks:
            self.call_request.add_life_cycle_callback(dispatch_constants.CALL_SUCCESS_LIFE_CYCLE_CALLBACK, h)
        self.task._run()
        for h in hooks:
            self.assertTrue(h.call_count == 1)
            self.assertTrue(h.call_args[0][0] is self.call_request)
            self.assertTrue(h.call_args[0][1] is self.call_report)

    def test_complete_execution_hook(self):
        hooks = [mock.Mock(), mock.Mock()]
        for h in hooks:
            self.call_request.add_life_cycle_callback(dispatch_constants.CALL_COMPLETE_LIFE_CYCLE_CALLBACK, h)
        self.task._run()
        for h in hooks:
            self.assertTrue(h.call_count == 1)
Ejemplo n.º 8
0
class TaskTests(base.PulpServerTests):
    def setUp(self):
        super(TaskTests, self).setUp()
        self.call_request = CallRequest(Call(), call_args, call_kwargs)
        self.call_report = CallReport()
        self.task = Task(self.call_request, self.call_report)

    def tearDown(self):
        super(TaskTests, self).tearDown()
        self.call_request = None
        self.call_report = None
        self.task = None

    def test_run(self):
        self.assertTrue(
            self.call_report.state is dispatch_constants.CALL_WAITING_STATE)
        self.task._run()
        self.assertTrue(self.call_request.call.call_count == 1)
        self.assertTrue(
            self.call_request.call.call_args[0] == call_args, '%s != %s' %
            (str(self.call_request.call.call_args[0]), str(call_args)))
        self.assertTrue(self.call_request.call.call_args[1] == call_kwargs)
        self.assertTrue(
            self.call_report.state is dispatch_constants.CALL_FINISHED_STATE)

    def test_complete(self):
        now = datetime.datetime.now(dateutils.utc_tz())
        self.task._run()
        self.assertTrue(self.call_report.finish_time > now)

    def test_complete_callback(self):
        callback = mock.Mock()
        self.task.complete_callback = callback
        self.task._run()
        self.assertTrue(callback.call_count == 1)
        self.assertTrue(callback.call_args[0][0] is self.task)

    def test_cancel_control_hook(self):
        callback = mock.Mock()
        self.call_request.add_control_hook(
            dispatch_constants.CALL_CANCEL_CONTROL_HOOK, callback)
        self.call_report.state = dispatch_constants.CALL_RUNNING_STATE
        try:
            self.task.cancel()
        except:
            self.fail(traceback.format_exc())
        self.assertTrue(callback.call_count == 1)
        self.assertTrue(callback.call_args[0][0] is self.call_request)
        self.assertTrue(callback.call_args[0][1] is self.call_report)
        self.assertTrue(
            self.call_report.state is dispatch_constants.CALL_CANCELED_STATE,
            self.call_report.state)

    def test_finish_execution_hook(self):
        hooks = [mock.Mock(), mock.Mock()]
        for h in hooks:
            self.call_request.add_life_cycle_callback(
                dispatch_constants.CALL_SUCCESS_LIFE_CYCLE_CALLBACK, h)
        self.task._run()
        for h in hooks:
            self.assertTrue(h.call_count == 1)
            self.assertTrue(h.call_args[0][0] is self.call_request)
            self.assertTrue(h.call_args[0][1] is self.call_report)

    def test_complete_execution_hook(self):
        hooks = [mock.Mock(), mock.Mock()]
        for h in hooks:
            self.call_request.add_life_cycle_callback(
                dispatch_constants.CALL_COMPLETE_LIFE_CYCLE_CALLBACK, h)
        self.task._run()
        for h in hooks:
            self.assertTrue(h.call_count == 1)