class FailTests(base.PulpServerTests): def setUp(self): self.call_request = CallRequest(fail) self.call_report = CallReport() self.task = Task(self.call_request, self.call_report) def tearDown(self): self.call_request = None self.call_report = None self.task = None def test_failed(self): self.task._run() self.assertTrue( self.call_report.state is dispatch_constants.CALL_ERROR_STATE, self.call_report.state) self.assertTrue(isinstance(self.call_report.exception, RuntimeError), str(type(self.call_report.exception))) self.assertTrue( isinstance(self.call_report.traceback, types.TracebackType)) def test_error_execution_hook(self): hook = mock.Mock() self.call_request.add_life_cycle_callback( dispatch_constants.CALL_FAILURE_LIFE_CYCLE_CALLBACK, hook) self.task._run() self.assertTrue(hook.call_count == 1) self.assertTrue(hook.call_args[0][0] is self.call_request) self.assertTrue(hook.call_args[0][1] is self.call_report)
def POST(self, repo_group_id): params = self.params() distributor_id = params.get('id', None) overrides = params.get('override_config', None) if distributor_id is None: raise MissingValue(['id']) publish_manager = managers_factory.repo_group_publish_manager() tags = [resource_tag(dispatch_constants.RESOURCE_REPOSITORY_GROUP_TYPE, repo_group_id), resource_tag(dispatch_constants.RESOURCE_REPOSITORY_GROUP_DISTRIBUTOR_TYPE, distributor_id), action_tag('publish') ] weight = pulp_config.config.getint('tasks', 'publish_weight') call_request = CallRequest(publish_manager.publish, args=[repo_group_id, distributor_id], kwargs={'publish_config_override' : overrides}, tags=tags, weight=weight, archive=True) call_request.updates_resource(dispatch_constants.RESOURCE_REPOSITORY_GROUP_TYPE, repo_group_id) call_request.updates_resource(dispatch_constants.RESOURCE_REPOSITORY_GROUP_DISTRIBUTOR_TYPE, distributor_id) call_request.add_life_cycle_callback(dispatch_constants.CALL_ENQUEUE_LIFE_CYCLE_CALLBACK, publish_manager.prep_publish) return execution.execute_async(self, call_request)
def test_serialize_deserialize_with_execution_hook(self): key = dispatch_constants.CALL_CANCEL_LIFE_CYCLE_CALLBACK call_request = CallRequest(function) call_request.add_life_cycle_callback(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.execution_hooks[key][0] == function)
def test_execution_hooks(self): call_request = CallRequest(function) for key in dispatch_constants.CALL_LIFE_CYCLE_CALLBACKS: self.assertTrue(isinstance(call_request.execution_hooks[key], list)) self.assertTrue(len(call_request.execution_hooks[key]) == 0) call_request.add_life_cycle_callback(key, function) self.assertTrue(isinstance(call_request.execution_hooks[key], list)) self.assertTrue(len(call_request.execution_hooks[key]) == 1)
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)
def bind_itinerary(consumer_id, repo_id, distributor_id, notify_agent, binding_config, agent_options): """ Get the bind itinerary: 1. Create the binding on the server. 2. Request that the consumer (agent) perform the bind. @param consumer_id: A consumer ID. @type consumer_id: str @param repo_id: A repository ID. @type repo_id: str @param distributor_id: A distributor ID. @type distributor_id: str @param agent_options: Bind options passed to the agent handler. @type agent_options: dict @param notify_agent: indicates if the agent should be sent a message about the new binding @type notify_agent: bool @param binding_config: configuration options to use when generating the payload for this binding @return: A list of call_requests. @rtype list """ call_requests = [] bind_manager = managers.consumer_bind_manager() agent_manager = managers.consumer_agent_manager() # bind tags = [ resource_tag(dispatch_constants.RESOURCE_CONSUMER_TYPE, consumer_id), resource_tag(dispatch_constants.RESOURCE_REPOSITORY_TYPE, repo_id), resource_tag(dispatch_constants.RESOURCE_REPOSITORY_DISTRIBUTOR_TYPE, distributor_id), action_tag(ACTION_BIND) ] args = [ consumer_id, repo_id, distributor_id, notify_agent, binding_config, ] bind_request = CallRequest(bind_manager.bind, args, weight=0, tags=tags) bind_request.reads_resource(dispatch_constants.RESOURCE_CONSUMER_TYPE, consumer_id) bind_request.reads_resource(dispatch_constants.RESOURCE_REPOSITORY_TYPE, repo_id) bind_request.reads_resource( dispatch_constants.RESOURCE_REPOSITORY_DISTRIBUTOR_TYPE, distributor_id) call_requests.append(bind_request) # notify agent if notify_agent: tags = [ resource_tag(dispatch_constants.RESOURCE_CONSUMER_TYPE, consumer_id), resource_tag(dispatch_constants.RESOURCE_REPOSITORY_TYPE, repo_id), resource_tag( dispatch_constants.RESOURCE_REPOSITORY_DISTRIBUTOR_TYPE, distributor_id), action_tag(ACTION_AGENT_BIND) ] args = [consumer_id, repo_id, distributor_id, agent_options] agent_request = CallRequest(agent_manager.bind, args, weight=0, asynchronous=True, archive=True, tags=tags) agent_request.add_life_cycle_callback( dispatch_constants.CALL_SUCCESS_LIFE_CYCLE_CALLBACK, bind_succeeded) agent_request.add_life_cycle_callback( dispatch_constants.CALL_FAILURE_LIFE_CYCLE_CALLBACK, bind_failed) call_requests.append(agent_request) agent_request.depends_on(bind_request.id) return call_requests
def unbind_itinerary(consumer_id, repo_id, distributor_id, options): """ Get the unbind itinerary. The tasks in the itinerary are as follows: 1. Mark the binding as (deleted) on the server. 2. Request that the consumer (agent) perform the unbind. 3. Delete the binding on the server. @param consumer_id: A consumer ID. @type consumer_id: str @param repo_id: A repository ID. @type repo_id: str @param distributor_id: A distributor ID. @type distributor_id: str @param options: Unbind options passed to the agent handler. @type options: dict @return: A list of call_requests. @rtype list """ call_requests = [] bind_manager = managers.consumer_bind_manager() agent_manager = managers.consumer_agent_manager() # unbind tags = [ resource_tag(dispatch_constants.RESOURCE_CONSUMER_TYPE, consumer_id), resource_tag(dispatch_constants.RESOURCE_REPOSITORY_TYPE, repo_id), resource_tag(dispatch_constants.RESOURCE_REPOSITORY_DISTRIBUTOR_TYPE, distributor_id), action_tag(ACTION_UNBIND) ] args = [ consumer_id, repo_id, distributor_id, ] unbind_request = CallRequest(bind_manager.unbind, args=args, tags=tags) unbind_request.reads_resource(dispatch_constants.RESOURCE_CONSUMER_TYPE, consumer_id) call_requests.append(unbind_request) # notify agent tags = [ resource_tag(dispatch_constants.RESOURCE_CONSUMER_TYPE, consumer_id), resource_tag(dispatch_constants.RESOURCE_REPOSITORY_TYPE, repo_id), resource_tag(dispatch_constants.RESOURCE_REPOSITORY_DISTRIBUTOR_TYPE, distributor_id), action_tag(ACTION_AGENT_UNBIND) ] args = [ consumer_id, repo_id, distributor_id, options, ] agent_request = CallRequest(agent_manager.unbind, args, weight=0, asynchronous=True, archive=True, tags=tags) agent_request.add_life_cycle_callback( dispatch_constants.CALL_SUCCESS_LIFE_CYCLE_CALLBACK, unbind_succeeded) agent_request.add_life_cycle_callback( dispatch_constants.CALL_FAILURE_LIFE_CYCLE_CALLBACK, unbind_failed) call_requests.append(agent_request) agent_request.depends_on(unbind_request.id) # delete the binding tags = [ resource_tag(dispatch_constants.RESOURCE_CONSUMER_TYPE, consumer_id), resource_tag(dispatch_constants.RESOURCE_REPOSITORY_TYPE, repo_id), resource_tag(dispatch_constants.RESOURCE_REPOSITORY_DISTRIBUTOR_TYPE, distributor_id), action_tag(ACTION_DELETE_BINDING) ] args = [consumer_id, repo_id, distributor_id] delete_request = CallRequest(bind_manager.delete, args=args, tags=tags) unbind_request.reads_resource(dispatch_constants.RESOURCE_CONSUMER_TYPE, consumer_id) call_requests.append(delete_request) delete_request.depends_on(agent_request.id) return call_requests