def test_no_action(self, mock_get_by_resource): mock_get_by_resource.return_value = self.calls result = self.manager.get('consumer1') mock_get_by_resource.assert_called_once_with(Consumer.build_resource_tag('consumer1')) self.assertEqual(result, self.calls)
def test_with_action(self, mock_get_by_resource): mock_get_by_resource.return_value = self.calls result = self.manager.get('consumer1', UNIT_INSTALL_ACTION) mock_get_by_resource.assert_called_once_with(Consumer.build_resource_tag('consumer1')) self.assertEqual(list(result), self.calls[:1])
def create_schedule(cls, action, consumer_id, units, options, schedule, failure_threshold=None, enabled=True): """ Creates a new schedule for a consumer action :param action: a unique identified for an action, one of UNIT_INSTALL_ACTION, UNIT_UPDATE_ACTION, UNIT_UNINSTALL_ACTION :type action: basestring :param consumer_id: a unique ID for a consumer :type consumer_id: basestring :param units: A list of content units to be installed, each as a dict in the form: { type_id:<str>, unit_key:<dict> } :type units: list :param options: a dictionary that will be passed to the action-appropriate task as the "options" argument :type options: dict :param schedule: ISO8601 string representation of the schedule :type schedule: basestring :param failure_threshold: optional positive integer indicating how many times this schedule's execution can fail before being automatically disabled. :type failure_threshold: int or NoneType :param enabled: boolean indicating if this schedule should be actively loaded and executed by the scheduler. Defaults to True. :type enabled: bool :return: instance of the new ScheduledCal :rtype: pulp.server.db.models.dispatch.ScheduledCall :raise: pulp.server.exceptions.MissingValue """ cls._validate_consumer(consumer_id) utils.validate_initial_schedule_options(schedule, failure_threshold, enabled) if not units: raise MissingValue(['units']) task = ACTIONS_TO_TASKS[action] args = [consumer_id] kwargs = {'units': units, 'options': options} resource = Consumer.build_resource_tag(consumer_id) schedule = ScheduledCall(schedule, task, args=args, kwargs=kwargs, resource=resource, failure_threshold=failure_threshold, enabled=enabled) schedule.save() return schedule
def test_with_action(self, mock_get_by_resource): mock_get_by_resource.return_value = self.calls result = self.manager.get('consumer1', UNIT_INSTALL_ACTION) mock_get_by_resource.assert_called_once_with( Consumer.build_resource_tag('consumer1')) self.assertEqual(list(result), self.calls[:1])
def test_no_action(self, mock_get_by_resource): mock_get_by_resource.return_value = self.calls result = self.manager.get('consumer1') mock_get_by_resource.assert_called_once_with( Consumer.build_resource_tag('consumer1')) self.assertEqual(result, self.calls)
def create_schedule(cls, action, consumer_id, units, options, schedule, failure_threshold=None, enabled=True): """ Creates a new schedule for a consumer action :param action: a unique identified for an action, one of UNIT_INSTALL_ACTION, UNIT_UPDATE_ACTION, UNIT_UNINSTALL_ACTION :type action: basestring :param consumer_id: a unique ID for a consumer :type consumer_id: basestring :param units: A list of content units to be installed, each as a dict in the form: { type_id:<str>, unit_key:<dict> } :type units: list :param options: a dictionary that will be passed to the action-appropriate task as the "options" argument :type options: dict :param schedule: ISO8601 string representation of the schedule :type schedule: basestring :param failure_threshold: optional positive integer indicating how many times this schedule's execution can fail before being automatically disabled. :type failure_threshold: int or NoneType :param enabled: boolean indicating if this schedule should be actively loaded and executed by the scheduler. Defaults to True. :type enabled: bool :return: instance of the new ScheduledCal :rtype: pulp.server.db.models.dispatch.ScheduledCall :raise: pulp.server.exceptions.MissingValue """ cls._validate_consumer(consumer_id) utils.validate_initial_schedule_options(schedule, failure_threshold, enabled) if not units: raise MissingValue(["units"]) task = ACTIONS_TO_TASKS[action] args = [consumer_id] kwargs = {"units": units, "options": options} resource = Consumer.build_resource_tag(consumer_id) schedule = ScheduledCall( schedule, task, args=args, kwargs=kwargs, resource=resource, failure_threshold=failure_threshold, enabled=enabled, ) schedule.save() return schedule
def test_save(self, mock_get_consumer, mock_save): iso_schedule = 'PT1H' result = self.manager.create_schedule(UNIT_INSTALL_ACTION, 'consumer1', self.units, {}, iso_schedule, 4, False) self.assertEqual(result.iso_schedule, iso_schedule) self.assertEqual(result.args, ['consumer1']) self.assertEqual(result.kwargs['units'], self.units) self.assertEqual(result.kwargs['options'], {}) self.assertEqual(result.resource, Consumer.build_resource_tag('consumer1')) self.assertTrue(result.enabled is False) mock_save.assert_called_once_with()
def get(consumer_id, action=None): """ Get a collection of schedules for the given consumer and action. If no action is specified, then all actions will be included. :param consumer_id: a unique ID for a consumer :type consumer_id: basestring :param action: a unique identifier for an action, one of UNIT_INSTALL_ACTION, UNIT_UPDATE_ACTION, UNIT_UNINSTALL_ACTION :type action: basestring :return: iterator of ScheduledCall instances :rtype: iterator """ results = utils.get_by_resource(Consumer.build_resource_tag(consumer_id)) if action: task = ACTIONS_TO_TASKS[action] return itertools.ifilter(lambda schedule: schedule.task == task.name, results) else: return results
def get(consumer_id, action=None): """ Get a collection of schedules for the given consumer and action. If no action is specified, then all actions will be included. :param consumer_id: a unique ID for a consumer :type consumer_id: basestring :param action: a unique identifier for an action, one of UNIT_INSTALL_ACTION, UNIT_UPDATE_ACTION, UNIT_UNINSTALL_ACTION :type action: basestring :return: iterator of ScheduledCall instances :rtype: iterator """ results = utils.get_by_resource( Consumer.build_resource_tag(consumer_id)) if action: task = ACTIONS_TO_TASKS[action] return itertools.ifilter( lambda schedule: schedule.task == task.name, results) else: return results