Example #1
0
    def __init__(self,
                 context,
                 name='remove',
                 description=DESC_REMOVE,
                 method=None,
                 type_id=None,
                 **kwargs):
        """
        :param context: client context
        :type  context: pulp.client.extensions.core.ClientContext
        :param name: The name of the command
        :type name: str
        :param description: the textual discription that will be displayed in the shell
        :type description: str
        :param method: method that will be fun when the command is invoked
        :type  method: function
        :param type_id: The type of units that this remove command supports
        :type type_id: str
        """

        # Handle odd constructor in UnitAssociationCriteriaCommand
        kwargs['name'] = name
        kwargs['description'] = description

        # We're not searching, we're using it to specify units
        kwargs['include_search'] = False

        method = method or self.run

        PollingCommand.__init__(self, name, description, method, context)
        UnitAssociationCriteriaCommand.__init__(self, method, **kwargs)

        self.type_id = type_id
        self.max_units_displayed = DISPLAY_UNITS_DEFAULT_MAXIMUM
Example #2
0
    def __init__(self, context, name='remove', description=DESC_REMOVE, method=None,
                 type_id=None, **kwargs):
        """
        :param context: client context
        :type  context: pulp.client.extensions.core.ClientContext
        :param name: The name of the command
        :type name: str
        :param description: the textual discription that will be displayed in the shell
        :type description: str
        :param method: method that will be fun when the command is invoked
        :type  method: function
        :param type_id: The type of units that this remove command supports
        :type type_id: str
        """

        # Handle odd constructor in UnitAssociationCriteriaCommand
        kwargs['name'] = name
        kwargs['description'] = description

        # We're not searching, we're using it to specify units
        kwargs['include_search'] = False

        method = method or self.run

        PollingCommand.__init__(self, name, description, method, context)
        UnitAssociationCriteriaCommand.__init__(self, method, **kwargs)

        self.type_id = type_id
        self.max_units_displayed = DISPLAY_UNITS_DEFAULT_MAXIMUM
Example #3
0
    def __init__(self,
                 context,
                 name='copy',
                 description=DESC_COPY,
                 method=None,
                 type_id=None,
                 **kwargs):

        # Handle odd constructor in UnitAssociationCriteriaCommand
        kwargs['name'] = name
        kwargs['description'] = description

        # We're not searching, we're using it to specify units
        kwargs['include_search'] = False

        method = method or self.run

        PollingCommand.__init__(self, name, description, method, context)
        UnitAssociationCriteriaCommand.__init__(self, method, **kwargs)

        self.type_id = type_id
        self.context = context
        self.prompt = context.prompt

        self.add_option(OPTION_FROM_REPO)
        self.add_option(OPTION_TO_REPO)
Example #4
0
    def setUp(self):
        super(PollingCommandTests, self).setUp()

        self.command = PollingCommand('poll',
                                      'desc',
                                      noop,
                                      self.context,
                                      poll_frequency_in_seconds=0)
Example #5
0
    def __init__(self, context, name=None, description=None):
        name = name or 'bind'
        description = description or _('binds a consumer to a repository')
        PollingCommand.__init__(self, name, description, self.run, context)

        self.add_option(OPTION_REPO_ID)
        self.add_consumer_option()
        self.add_distributor_option()
Example #6
0
File: bind.py Project: ashcrow/pulp
    def __init__(self, context, name=None, description=None):
        name = name or 'bind'
        description = description or _('binds a consumer to a repository')
        PollingCommand.__init__(self, name, description, self.run, context)

        self.add_option(OPTION_REPO_ID)
        self.add_consumer_option()
        self.add_distributor_option()
Example #7
0
 def __init__(self, context):
     PollingCommand.__init__(self, 'uninstall', _('uninstall packages'),
                             self.run, context)
     self.create_option('--name',
                        _('package name; may repeat for multiple packages'),
                        required=True,
                        allow_multiple=True,
                        aliases=['-n'])
Example #8
0
 def __init__(self, context):
     PollingCommand.__init__(self, 'install', _('install packages'),
                             self.run, context)
     self.create_option('--name',
                        _('package name; may repeat for multiple packages'),
                        required=True,
                        allow_multiple=True,
                        aliases=['-n'])
     self.create_flag('--import-keys', _('import GPG keys as needed'))
Example #9
0
File: bind.py Project: ashcrow/pulp
    def __init__(self, context, name=None, description=None):
        name = name or 'unbind'
        description = description or _('removes the binding between a consumer and a repository')
        PollingCommand.__init__(self, name, description, self.run, context)

        self.add_option(OPTION_REPO_ID)
        self.add_consumer_option()
        self.add_distributor_option()

        self.add_flag(FLAG_FORCE)
Example #10
0
    def __init__(self, context, name=None, description=None):
        name = name or 'unbind'
        description = description or _(
            'removes the binding between a consumer and a repository')
        PollingCommand.__init__(self, name, description, self.run, context)

        self.add_option(OPTION_REPO_ID)
        self.add_consumer_option()
        self.add_distributor_option()

        self.add_flag(FLAG_FORCE)
 def __init__(self, context):
     PollingCommand.__init__(
         self,
         'uninstall',
         _('uninstall packages'),
         self.run,
         context)
     self.create_option(
         '--name',
         _('package name; may repeat for multiple packages'),
         required=True,
         allow_multiple=True,
         aliases=['-n'])
Example #12
0
    def __init__(self, context, name='remove', description=DESC_REMOVE, method=None, type_id=None, **kwargs):

        # Handle odd constructor in UnitAssociationCriteriaCommand
        kwargs['name'] = name
        kwargs['description'] = description

        # We're not searching, we're using it to specify units
        kwargs['include_search'] = False

        method = method or self.run

        PollingCommand.__init__(self, name, description, method, context)
        UnitAssociationCriteriaCommand.__init__(self, method, **kwargs)

        self.type_id = type_id
Example #13
0
    def test_init_load_poll_frequency(self):
        # Test
        command = PollingCommand('poll', 'desc', noop, self.context)

        # Verify
        self.assertEqual(.5,
                         command.poll_frequency_in_seconds)  # from defaults
 def __init__(self, context):
     PollingCommand.__init__(
         self,
         'install',
         _('install packages'),
         self.run,
         context)
     self.create_option(
         '--name',
         _('package name; may repeat for multiple packages'),
         required=True,
         allow_multiple=True,
         aliases=['-n'])
     self.create_flag(
         '--import-keys',
         _('import GPG keys as needed'))
Example #15
0
File: unit.py Project: bartwo/pulp
    def __init__(self, context, name="copy", description=DESC_COPY, method=None, type_id=None, **kwargs):

        # Handle odd constructor in UnitAssociationCriteriaCommand
        kwargs["name"] = name
        kwargs["description"] = description

        # We're not searching, we're using it to specify units
        kwargs["include_search"] = False

        method = method or self.run

        PollingCommand.__init__(self, name, description, method, context)
        UnitAssociationCriteriaCommand.__init__(self, method, **kwargs)

        self.type_id = type_id
        self.context = context
        self.prompt = context.prompt

        self.add_option(OPTION_FROM_REPO)
        self.add_option(OPTION_TO_REPO)
Example #16
0
    def __init__(self,
                 context,
                 name='remove',
                 description=DESC_REMOVE,
                 method=None,
                 type_id=None,
                 **kwargs):

        # Handle odd constructor in UnitAssociationCriteriaCommand
        kwargs['name'] = name
        kwargs['description'] = description

        # We're not searching, we're using it to specify units
        kwargs['include_search'] = False

        method = method or self.run

        PollingCommand.__init__(self, name, description, method, context)
        UnitAssociationCriteriaCommand.__init__(self, method, **kwargs)

        self.type_id = type_id
Example #17
0
class PollingCommandTests(base.PulpClientTests):

    def setUp(self):
        super(PollingCommandTests, self).setUp()

        self.command = PollingCommand('poll', 'desc', noop, self.context, poll_frequency_in_seconds=0)

    def test_init_load_poll_frequency(self):
        # Test
        command = PollingCommand('poll', 'desc', noop, self.context)

        # Verify
        self.assertEqual(.5, command.poll_frequency_in_seconds)  # from test-override-admin.conf

    @mock.patch('time.sleep')
    def test_poll_single_task(self, mock_sleep):
        """
        Task Count: 1
        Statuses: None; normal progression of waiting to running to completed
        Result: Success

        This test verifies the sleep and progress callback calls, which will be omitted
        in most other tests cases where appropriate.
        """

        # Setup
        sim = TaskSimulator()
        sim.install(self.bindings)

        task_id = '123'
        state_progression = [STATE_WAITING,
                             STATE_WAITING,
                             STATE_RUNNING,
                             STATE_RUNNING,
                             STATE_FINISHED]
        sim.add_task_states(task_id, state_progression)

        mock_progress_call = mock.MagicMock().progress
        self.command.progress = mock_progress_call

        # Test
        task_list = sim.get_all_tasks().response_body
        completed_tasks = self.command.poll(task_list, {})

        # Verify

        # The "header" tag should not be present since no headers are needed for single tasks

        expected_tags = ['abort', 'delayed-spinner', 'delayed-spinner', 'succeeded']
        self.assertEqual(self.prompt.get_write_tags(), expected_tags)

        self.assertEqual(4, mock_sleep.call_count) # 2 for waiting, 2 for running
        self.assertEqual(mock_sleep.call_args_list[0][0][0], 0)  # frequency passed to sleep

        self.assertEqual(3, mock_progress_call.call_count) # 2 running, 1 final

        self.assertTrue(isinstance(completed_tasks, list))
        self.assertEqual(1, len(completed_tasks))
        self.assertEqual(STATE_FINISHED, completed_tasks[0].state)

    def test_poll_task_list(self):
        """
        Task Count: 3
        Statuses: None; normal progression
        Result: All Success
        """

        # Setup
        sim = TaskSimulator()
        sim.install(self.bindings)

        states_1 = [STATE_WAITING, STATE_RUNNING, STATE_FINISHED]
        states_2 = [STATE_WAITING, STATE_WAITING, STATE_RUNNING, STATE_FINISHED]
        states_3 = [STATE_WAITING, STATE_RUNNING, STATE_RUNNING, STATE_RUNNING, STATE_FINISHED]

        sim.add_task_states('1', states_1)
        sim.add_task_states('2', states_2)
        sim.add_task_states('3', states_3)

        # Test
        task_list = sim.get_all_tasks().response_body
        completed_tasks = self.command.poll(task_list, {})

        expected_tags = ['abort', # default, always displayed
                         # states_1
                         'header', 'delayed-spinner', 'running-spinner', 'running-spinner', 'succeeded',
                         # states_2
                         'header', 'delayed-spinner', 'delayed-spinner', 'running-spinner', 'running-spinner',
                         'succeeded',
                         # states_3
                         'header', 'delayed-spinner', 'running-spinner', 'running-spinner',
                         'running-spinner', 'running-spinner', 'succeeded',
                         ]
        found_tags = self.prompt.get_write_tags()
        self.assertEqual(expected_tags, found_tags)

        self.assertTrue(isinstance(completed_tasks, list))
        self.assertEqual(3, len(completed_tasks))
        for i in range(0, 3):
            self.assertEqual(STATE_FINISHED, completed_tasks[i].state)

    def test_poll_spawned_tasks_list(self):
        """
        Test the structure where a command has both synchronous and asynchronous sections
        and returns a task structure with a result and a spawned_tasks list

        Task Count: 3
        Statuses: None; normal progression
        Result: All Success
        """

        # Setup
        sim = TaskSimulator()
        sim.install(self.bindings)

        states_1 = [STATE_WAITING, STATE_RUNNING, STATE_FINISHED]
        states_2 = [STATE_WAITING, STATE_WAITING, STATE_RUNNING, STATE_FINISHED]
        states_3 = [STATE_WAITING, STATE_RUNNING, STATE_RUNNING, STATE_RUNNING, STATE_FINISHED]

        sim.add_task_states('1', states_1)
        sim.add_task_states('2', states_2)
        sim.add_task_states('3', states_3)

        container_task = Task({})

        # Test
        container_task.spawned_tasks = sim.get_all_tasks().response_body
        completed_tasks = self.command.poll(container_task, {})

        expected_tags = ['abort', # default, always displayed
                         # states_1
                         'header', 'delayed-spinner', 'running-spinner', 'running-spinner', 'succeeded',
                         # states_2
                         'header', 'delayed-spinner', 'delayed-spinner', 'running-spinner', 'running-spinner',
                         'succeeded',
                         # states_3
                         'header', 'delayed-spinner', 'running-spinner', 'running-spinner',
                         'running-spinner', 'running-spinner', 'succeeded',
                         ]
        found_tags = self.prompt.get_write_tags()
        self.assertEqual(expected_tags, found_tags)

        self.assertTrue(isinstance(completed_tasks, list))
        self.assertEqual(3, len(completed_tasks))
        for i in range(0, 3):
            self.assertEqual(STATE_FINISHED, completed_tasks[i].state)

    def test_poll_additional_spawned_tasks_list(self):
        """
        Test polling over a list where a task has spawned additional tasks that need to be
        added to the polling list

        Task Count: 3
        Statuses: None; normal progression
        Result: All Success
        """

        # Setup
        sim = TaskSimulator()
        sim.install(self.bindings)

        states_1 = [STATE_WAITING, STATE_RUNNING, STATE_FINISHED]
        states_2 = [STATE_WAITING, STATE_WAITING, STATE_RUNNING, STATE_FINISHED]
        states_3 = [STATE_WAITING, STATE_RUNNING, STATE_RUNNING, STATE_RUNNING, STATE_FINISHED]

        task_1_states = sim.add_task_states('1', states_1)
        sim.add_task_states('2', states_2)
        sim.add_task_states('3', states_3)

        container_task = Task({})
        task_list = sim.get_all_tasks().response_body
        task_1_states[2].spawned_tasks = task_list[1:]
        # Test
        container_task.spawned_tasks = sim.get_all_tasks().response_body
        completed_tasks = self.command.poll(task_list[:1], {})

        expected_tags = ['abort', # default, always displayed
                         # states_1
                         'delayed-spinner', 'running-spinner', 'succeeded',
                         # states_2
                         'header', 'delayed-spinner', 'running-spinner', 'running-spinner',
                         'succeeded',
                         # states_3
                         'header', 'delayed-spinner', 'running-spinner', 'running-spinner',
                         'running-spinner',  'succeeded',
                         ]
        found_tags = self.prompt.get_write_tags()
        self.assertEqual(expected_tags, found_tags)

        self.assertTrue(isinstance(completed_tasks, list))
        self.assertEqual(3, len(completed_tasks))
        for i in range(0, 3):
            self.assertEqual(STATE_FINISHED, completed_tasks[i].state)

    def test_get_tasks_to_poll_duplicate_tasks(self):
        sim = TaskSimulator()
        sim.add_task_state('1', STATE_FINISHED)

        task_list = sim.get_all_tasks().response_body
        tasks_to_poll = self.command._get_tasks_to_poll([task_list[0], task_list[0]])
        self.assertEquals(1, len(tasks_to_poll))

    def test_get_tasks_to_poll_nested_tasks(self):
        sim = TaskSimulator()
        sim.add_task_state('1', STATE_FINISHED)
        sim.add_task_state('2', STATE_FINISHED)

        task_list = sim.get_all_tasks().response_body
        source_task = task_list[0]
        nested_task = task_list[1]
        source_task.spawned_tasks = [nested_task]
        tasks_to_poll = self.command._get_tasks_to_poll(source_task)
        self.assertEquals(2, len(tasks_to_poll))

    def test_get_tasks_to_poll_source_task_list(self):
        sim = TaskSimulator()
        sim.add_task_state('1', STATE_FINISHED)
        sim.add_task_state('2', STATE_FINISHED)

        task_list = sim.get_all_tasks().response_body
        source_task = task_list[0]
        nested_task = task_list[1]
        source_task.spawned_tasks = [nested_task]
        tasks_to_poll = self.command._get_tasks_to_poll([source_task])
        self.assertEquals(2, len(tasks_to_poll))

    def test_get_tasks_to_poll_raises_type_error(self):
        self.assertRaises(TypeError, self.command._get_tasks_to_poll, 'foo')

    def test_poll_background(self):
        # Setup
        sim = TaskSimulator()
        sim.add_task_state('1', STATE_FINISHED)

        # Test
        task_list = sim.get_all_tasks().response_body
        result = self.command.poll(task_list, {FLAG_BACKGROUND.keyword : True})

        # Verify
        self.assertEqual(result, RESULT_BACKGROUND)

    def test_poll_empty_list(self):
        # Test
        completed_tasks = self.command.poll([], {})

        # Verify
        #   The poll command shouldn't output anything and instead just end.
        self.assertEqual(0, len(self.prompt.get_write_tags()))
        self.assertTrue(isinstance(completed_tasks, list))
        self.assertEqual(0, len(completed_tasks))

    def test_failed_task(self):
        """
        Task Count: 3
        Statuses: None, tasks will run to completion
        Results: 1 Success, 1 Failed, 1 Skipped
        """

        # Setup
        sim = TaskSimulator()
        sim.install(self.bindings)

        states_1 = [STATE_WAITING, STATE_FINISHED]
        states_2 = [STATE_WAITING, STATE_ERROR]
        states_3 = [STATE_WAITING, STATE_SKIPPED]

        sim.add_task_states('1', states_1)
        sim.add_task_states('2', states_2)
        sim.add_task_states('3', states_3)

        # Test
        task_list = sim.get_all_tasks().response_body
        completed_tasks = self.command.poll(task_list, {})

        # Verify
        self.assertTrue(isinstance(completed_tasks, list))
        self.assertEqual(2, len(completed_tasks))

        expected_tags = ['abort',
                         'header', 'delayed-spinner', 'running-spinner', 'succeeded', # states_1
                         'header', 'delayed-spinner', 'running-spinner', 'failed', # states_2
                         ]
        self.assertEqual(expected_tags, self.prompt.get_write_tags())

    def test_cancelled_task(self):
        """
        Task Count: 1
        Statuses: Cancelled after 2 in progress polls
        Results: 1 Cancelled
        """

        # Setup
        sim = TaskSimulator()
        sim.install(self.bindings)

        states = [STATE_WAITING, STATE_RUNNING, STATE_RUNNING, STATE_CANCELED]
        sim.add_task_states('1', states)

        # Test
        task_list = sim.get_all_tasks().response_body
        completed_tasks = self.command.poll(task_list, {})

        # Verify
        self.assertTrue(isinstance(completed_tasks, list))
        self.assertEqual(1, len(completed_tasks))

        expected_tags = ['abort', 'delayed-spinner', 'running-spinner', 'running-spinner',
                         'running-spinner','cancelled']
        self.assertEqual(expected_tags, self.prompt.get_write_tags())

    def test_keyboard_interrupt(self):
        # Setup
        mock_poll_call = mock.MagicMock()._poll_task
        mock_poll_call.side_effect = KeyboardInterrupt()
        self.command._poll_task = mock_poll_call

        sim = TaskSimulator()
        sim.install(self.bindings)

        sim.add_task_state('1', STATE_WAITING)

        # Test
        task_list = sim.get_all_tasks().response_body
        result = self.command.poll(task_list, {})

        # Verify
        self.assertEqual(result, RESULT_ABORTED)

        self.assertEqual(['abort'], self.prompt.get_write_tags())
Example #18
0
    def setUp(self):
        super(PollingCommandTests, self).setUp()

        self.command = PollingCommand('poll', 'desc', noop, self.context, poll_frequency_in_seconds=0)
Example #19
0
class PollingCommandTests(base.PulpClientTests):

    def setUp(self):
        super(PollingCommandTests, self).setUp()

        self.command = PollingCommand('poll', 'desc', noop, self.context, poll_frequency_in_seconds=0)

    def test_init_load_poll_frequency(self):
        # Test
        command = PollingCommand('poll', 'desc', noop, self.context)

        # Verify
        self.assertEqual(.5, command.poll_frequency_in_seconds)  # from test-override-admin.conf

    @mock.patch('time.sleep')
    def test_poll_single_task(self, mock_sleep):
        """
        Task Count: 1
        Statuses: None; normal progression of waiting to running to completed
        Result: Success

        This test verifies the sleep and progress callback calls, which will be omitted
        in most other tests cases where appropriate.
        """

        # Setup
        sim = TaskSimulator()
        sim.install(self.bindings)

        task_id = '123'
        state_progression = [STATE_WAITING,
                             STATE_WAITING,
                             STATE_RUNNING,
                             STATE_RUNNING,
                             STATE_FINISHED]
        sim.add_task_states(task_id, state_progression)

        mock_progress_call = mock.MagicMock().progress
        self.command.progress = mock_progress_call

        # Test
        task_list = sim.get_all_tasks().response_body
        completed_tasks = self.command.poll(task_list, {})

        # Verify

        # The "header" tag should not be present since no headers are needed for single tasks

        expected_tags = ['abort', 'delayed-spinner', 'delayed-spinner', 'succeeded']
        self.assertEqual(self.prompt.get_write_tags(), expected_tags)

        self.assertEqual(4, mock_sleep.call_count) # 2 for waiting, 2 for running
        self.assertEqual(mock_sleep.call_args_list[0][0][0], 0)  # frequency passed to sleep

        self.assertEqual(3, mock_progress_call.call_count) # 2 running, 1 final

        self.assertTrue(isinstance(completed_tasks, list))
        self.assertEqual(1, len(completed_tasks))
        self.assertEqual(STATE_FINISHED, completed_tasks[0].state)

    def test_poll_task_list(self):
        """
        Task Count: 3
        Statuses: None; normal progression
        Result: All Success
        """

        # Setup
        sim = TaskSimulator()
        sim.install(self.bindings)

        states_1 = [STATE_WAITING, STATE_RUNNING, STATE_FINISHED]
        states_2 = [STATE_WAITING, STATE_WAITING, STATE_RUNNING, STATE_FINISHED]
        states_3 = [STATE_WAITING, STATE_RUNNING, STATE_RUNNING, STATE_RUNNING, STATE_FINISHED]

        sim.add_task_states('1', states_1)
        sim.add_task_states('2', states_2)
        sim.add_task_states('3', states_3)

        # Test
        task_list = sim.get_all_tasks().response_body
        completed_tasks = self.command.poll(task_list, {})

        expected_tags = ['abort', # default, always displayed
                         # states_1
                         'header', 'delayed-spinner', 'running-spinner', 'running-spinner', 'succeeded',
                         # states_2
                         'header', 'delayed-spinner', 'delayed-spinner', 'running-spinner', 'running-spinner',
                         'succeeded',
                         # states_3
                         'header', 'delayed-spinner', 'running-spinner', 'running-spinner',
                         'running-spinner', 'running-spinner', 'succeeded',
                         ]
        found_tags = self.prompt.get_write_tags()
        self.assertEqual(expected_tags, found_tags)

        self.assertTrue(isinstance(completed_tasks, list))
        self.assertEqual(3, len(completed_tasks))
        for i in range(0, 3):
            self.assertEqual(STATE_FINISHED, completed_tasks[i].state)

    def test_poll_background(self):
        # Setup
        sim = TaskSimulator()
        sim.add_task_state('1', STATE_FINISHED)

        # Test
        task_list = sim.get_all_tasks().response_body
        result = self.command.poll(task_list, {FLAG_BACKGROUND.keyword : True})

        # Verify
        self.assertEqual(result, RESULT_BACKGROUND)

    def test_poll_empty_list(self):
        # Test
        completed_tasks = self.command.poll([], {})

        # Verify
        #   The poll command shouldn't output anything and instead just end.
        self.assertEqual(0, len(self.prompt.get_write_tags()))
        self.assertTrue(isinstance(completed_tasks, list))
        self.assertEqual(0, len(completed_tasks))

    def test_poll_rejected(self):
        """
        Task Count: 2
        Statuses: Rejected (according to server behavior, all will be rejected if one is)
        """

        # Setup
        sim = TaskSimulator()
        sim.install(self.bindings)

        sim.add_task_state('1', STATE_ERROR, response=RESPONSE_REJECTED)
        sim.add_task_state('2', STATE_ERROR, response=RESPONSE_REJECTED)

        # Test
        task_list = sim.get_all_tasks().response_body
        completed_tasks = self.command.poll(task_list, {})

        # Verify
        self.assertEqual(completed_tasks, RESULT_REJECTED)

        self.assertEqual(self.prompt.get_write_tags(), ['rejected'])

    def test_failed_task(self):
        """
        Task Count: 3
        Statuses: None, tasks will run to completion
        Results: 1 Success, 1 Failed, 1 Skipped
        """

        # Setup
        sim = TaskSimulator()
        sim.install(self.bindings)

        states_1 = [STATE_WAITING, STATE_FINISHED]
        states_2 = [STATE_WAITING, STATE_ERROR]
        states_3 = [STATE_WAITING, STATE_SKIPPED]

        sim.add_task_states('1', states_1)
        sim.add_task_states('2', states_2)
        sim.add_task_states('3', states_3)

        # Test
        task_list = sim.get_all_tasks().response_body
        completed_tasks = self.command.poll(task_list, {})

        # Verify
        self.assertTrue(isinstance(completed_tasks, list))
        self.assertEqual(2, len(completed_tasks))

        expected_tags = ['abort',
                         'header', 'delayed-spinner', 'running-spinner', 'succeeded', # states_1
                         'header', 'delayed-spinner', 'running-spinner', 'failed', 'failed_exception', # states_2
                         ]
        self.assertEqual(expected_tags, self.prompt.get_write_tags())

    def test_cancelled_task(self):
        """
        Task Count: 1
        Statuses: Cancelled after 2 in progress polls
        Results: 1 Cancelled
        """

        # Setup
        sim = TaskSimulator()
        sim.install(self.bindings)

        states = [STATE_WAITING, STATE_RUNNING, STATE_RUNNING, STATE_CANCELED]
        sim.add_task_states('1', states)

        # Test
        task_list = sim.get_all_tasks().response_body
        completed_tasks = self.command.poll(task_list, {})

        # Verify
        self.assertTrue(isinstance(completed_tasks, list))
        self.assertEqual(1, len(completed_tasks))

        expected_tags = ['abort', 'delayed-spinner', 'running-spinner', 'running-spinner',
                         'running-spinner','cancelled']
        self.assertEqual(expected_tags, self.prompt.get_write_tags())

    def test_postponed_task(self):
        """
        Task Count: 1
        Statuses: Postponed (test will eventually run it)
        Results: 1 Success
        """

        # Setup
        sim = TaskSimulator()
        sim.install(self.bindings)

        # Remain postponed for two polls and then runs successfully
        sim.add_task_state('1', STATE_WAITING, response=RESPONSE_POSTPONED)
        sim.add_task_state('1', STATE_WAITING, response=RESPONSE_POSTPONED)
        states = [STATE_RUNNING, STATE_FINISHED]
        sim.add_task_states('1', states)

        # Test
        task_list = sim.get_all_tasks().response_body
        completed_tasks = self.command.poll(task_list, {})

        # Verify
        self.assertTrue(isinstance(completed_tasks, list))
        self.assertEqual(1, len(completed_tasks))

        expected_tags = ['abort', 'delayed-spinner', 'delayed-spinner', 'running-spinner',
                         'running-spinner', 'succeeded']
        self.assertEqual(expected_tags, self.prompt.get_write_tags())

    def test_keyboard_interrupt(self):
        # Setup
        mock_poll_call = mock.MagicMock()._poll_task
        mock_poll_call.side_effect = KeyboardInterrupt()
        self.command._poll_task = mock_poll_call

        sim = TaskSimulator()
        sim.install(self.bindings)

        sim.add_task_state('1', STATE_WAITING)

        # Test
        task_list = sim.get_all_tasks().response_body
        result = self.command.poll(task_list, {})

        # Verify
        self.assertEqual(result, RESULT_ABORTED)

        self.assertEqual(['abort'], self.prompt.get_write_tags())
Example #20
0
class PollingCommandTests(base.PulpClientTests):
    def setUp(self):
        super(PollingCommandTests, self).setUp()

        self.command = PollingCommand('poll',
                                      'desc',
                                      noop,
                                      self.context,
                                      poll_frequency_in_seconds=0)

    def test_init_load_poll_frequency(self):
        # Test
        command = PollingCommand('poll', 'desc', noop, self.context)

        # Verify
        self.assertEqual(.5,
                         command.poll_frequency_in_seconds)  # from defaults

    @mock.patch('time.sleep')
    def test_poll_single_task(self, mock_sleep):
        """
        Task Count: 1
        Statuses: None; normal progression of waiting to running to completed
        Result: Success

        This test verifies the sleep and progress callback calls, which will be omitted
        in most other tests cases where appropriate.
        """

        # Setup
        sim = TaskSimulator()
        sim.install(self.bindings)

        task_id = '123'
        state_progression = [
            STATE_WAITING, STATE_ACCEPTED, STATE_RUNNING, STATE_RUNNING,
            STATE_FINISHED
        ]
        sim.add_task_states(task_id, state_progression)

        mock_progress_call = mock.MagicMock().progress
        self.command.progress = mock_progress_call

        # Test
        task_list = sim.get_all_tasks().response_body
        completed_tasks = self.command.poll(task_list, {})

        # Verify

        # The "header" tag should not be present since no headers are needed for single tasks

        expected_tags = [
            'abort', 'delayed-spinner', 'delayed-spinner', 'succeeded'
        ]
        self.assertEqual(self.prompt.get_write_tags(), expected_tags)

        self.assertEqual(4,
                         mock_sleep.call_count)  # 2 for waiting, 2 for running
        self.assertEqual(mock_sleep.call_args_list[0][0][0],
                         0)  # frequency passed to sleep

        self.assertEqual(3,
                         mock_progress_call.call_count)  # 2 running, 1 final

        self.assertTrue(isinstance(completed_tasks, list))
        self.assertEqual(1, len(completed_tasks))
        self.assertEqual(STATE_FINISHED, completed_tasks[0].state)

    def test_poll_task_list(self):
        """
        Task Count: 3
        Statuses: None; normal progression
        Result: All Success
        """

        # Setup
        sim = TaskSimulator()
        sim.install(self.bindings)

        states_1 = [STATE_WAITING, STATE_RUNNING, STATE_FINISHED]
        states_2 = [
            STATE_WAITING, STATE_WAITING, STATE_RUNNING, STATE_FINISHED
        ]
        states_3 = [
            STATE_WAITING, STATE_RUNNING, STATE_RUNNING, STATE_RUNNING,
            STATE_FINISHED
        ]

        sim.add_task_states('1', states_1)
        sim.add_task_states('2', states_2)
        sim.add_task_states('3', states_3)

        # Test
        task_list = sim.get_all_tasks().response_body
        completed_tasks = self.command.poll(task_list, {})

        expected_tags = [
            'abort',  # default, always displayed
            # states_1
            'header',
            'delayed-spinner',
            'running-spinner',
            'running-spinner',
            'succeeded',
            # states_2
            'header',
            'delayed-spinner',
            'delayed-spinner',
            'running-spinner',
            'running-spinner',
            'succeeded',
            # states_3
            'header',
            'delayed-spinner',
            'running-spinner',
            'running-spinner',
            'running-spinner',
            'running-spinner',
            'succeeded'
        ]
        found_tags = self.prompt.get_write_tags()
        self.assertEqual(set(expected_tags), set(found_tags))

        self.assertTrue(isinstance(completed_tasks, list))
        self.assertEqual(3, len(completed_tasks))
        for i in range(0, 3):
            self.assertEqual(STATE_FINISHED, completed_tasks[i].state)

    def test_poll_spawned_tasks_list(self):
        """
        Test the structure where a command has both synchronous and asynchronous sections
        and returns a task structure with a result and a spawned_tasks list

        Task Count: 3
        Statuses: None; normal progression
        Result: All Success
        """

        # Setup
        sim = TaskSimulator()
        sim.install(self.bindings)

        states_1 = [STATE_WAITING, STATE_RUNNING, STATE_FINISHED]
        states_2 = [
            STATE_WAITING, STATE_WAITING, STATE_RUNNING, STATE_FINISHED
        ]
        states_3 = [
            STATE_WAITING, STATE_RUNNING, STATE_RUNNING, STATE_RUNNING,
            STATE_FINISHED
        ]

        sim.add_task_states('1', states_1)
        sim.add_task_states('2', states_2)
        sim.add_task_states('3', states_3)

        container_task = Task({})

        # Test
        container_task.spawned_tasks = sim.get_all_tasks().response_body
        completed_tasks = self.command.poll(container_task, {})

        expected_tags = [
            'abort',  # default, always displayed
            # states_1
            'header',
            'delayed-spinner',
            'running-spinner',
            'running-spinner',
            'succeeded',
            # states_2
            'header',
            'delayed-spinner',
            'delayed-spinner',
            'running-spinner',
            'running-spinner',
            'succeeded',
            # states_3
            'header',
            'delayed-spinner',
            'running-spinner',
            'running-spinner',
            'running-spinner',
            'running-spinner',
            'succeeded'
        ]
        found_tags = self.prompt.get_write_tags()
        self.assertEqual(set(expected_tags), set(found_tags))

        self.assertTrue(isinstance(completed_tasks, list))
        self.assertEqual(3, len(completed_tasks))
        for i in range(0, 3):
            self.assertEqual(STATE_FINISHED, completed_tasks[i].state)

    def test_poll_additional_spawned_tasks_list(self):
        """
        Test polling over a list where a task has spawned additional tasks that need to be
        added to the polling list

        Task Count: 3
        Statuses: None; normal progression
        Result: All Success
        """

        # Setup
        sim = TaskSimulator()
        sim.install(self.bindings)

        states_1 = [STATE_WAITING, STATE_RUNNING, STATE_FINISHED]
        states_2 = [
            STATE_WAITING, STATE_WAITING, STATE_RUNNING, STATE_FINISHED
        ]
        states_3 = [
            STATE_WAITING, STATE_RUNNING, STATE_RUNNING, STATE_RUNNING,
            STATE_FINISHED
        ]

        task_1_states = sim.add_task_states('1', states_1)
        sim.add_task_states('2', states_2)
        sim.add_task_states('3', states_3)

        container_task = Task({})
        task_list = sim.get_all_tasks().response_body
        task_1_states[2].spawned_tasks = task_list[1:]
        # Test
        container_task.spawned_tasks = sim.get_all_tasks().response_body
        completed_tasks = self.command.poll(task_list[:1], {})

        expected_tags = [
            'abort',  # default, always displayed
            # states_1
            'delayed-spinner',
            'running-spinner',
            'succeeded',
            # states_2
            'header',
            'delayed-spinner',
            'running-spinner',
            'running-spinner',
            'succeeded',
            # states_3
            'header',
            'delayed-spinner',
            'running-spinner',
            'running-spinner',
            'running-spinner',
            'succeeded'
        ]
        found_tags = self.prompt.get_write_tags()
        self.assertEqual(set(expected_tags), set(found_tags))

        self.assertTrue(isinstance(completed_tasks, list))
        self.assertEqual(3, len(completed_tasks))
        for i in range(0, 3):
            self.assertEqual(STATE_FINISHED, completed_tasks[i].state)

    def test_get_tasks_to_poll_duplicate_tasks(self):
        sim = TaskSimulator()
        sim.add_task_state('1', STATE_FINISHED)

        task_list = sim.get_all_tasks().response_body
        tasks_to_poll = self.command._get_tasks_to_poll(
            [task_list[0], task_list[0]])
        self.assertEquals(1, len(tasks_to_poll))

    def test_get_tasks_to_poll_nested_tasks(self):
        sim = TaskSimulator()
        sim.add_task_state('1', STATE_FINISHED)
        sim.add_task_state('2', STATE_FINISHED)

        task_list = sim.get_all_tasks().response_body
        source_task = task_list[0]
        nested_task = task_list[1]
        source_task.spawned_tasks = [nested_task]
        tasks_to_poll = self.command._get_tasks_to_poll(source_task)
        self.assertEquals(2, len(tasks_to_poll))

    def test_get_tasks_to_poll_source_task_list(self):
        sim = TaskSimulator()
        sim.add_task_state('1', STATE_FINISHED)
        sim.add_task_state('2', STATE_FINISHED)

        task_list = sim.get_all_tasks().response_body
        source_task = task_list[0]
        nested_task = task_list[1]
        source_task.spawned_tasks = [nested_task]
        tasks_to_poll = self.command._get_tasks_to_poll([source_task])
        self.assertEquals(2, len(tasks_to_poll))

    def test_get_tasks_to_poll_raises_type_error(self):
        self.assertRaises(TypeError, self.command._get_tasks_to_poll, 'foo')

    def test_poll_background(self):
        # Setup
        sim = TaskSimulator()
        sim.add_task_state('1', STATE_FINISHED)

        # Test
        task_list = sim.get_all_tasks().response_body
        result = self.command.poll(task_list, {FLAG_BACKGROUND.keyword: True})

        # Verify
        self.assertEqual(result, RESULT_BACKGROUND)

    def test_poll_empty_list(self):
        # Test
        completed_tasks = self.command.poll([], {})

        # Verify
        #   The poll command shouldn't output anything and instead just end.
        self.assertEqual(0, len(self.prompt.get_write_tags()))
        self.assertTrue(isinstance(completed_tasks, list))
        self.assertEqual(0, len(completed_tasks))

    def test_failed_task(self):
        """
        Task Count: 3
        Statuses: None, tasks will run to completion
        Results: 1 Success, 1 Failed, 1 Skipped
        """

        # Setup
        sim = TaskSimulator()
        sim.install(self.bindings)

        states_1 = [STATE_WAITING, STATE_FINISHED]
        states_2 = [STATE_WAITING, STATE_ERROR]
        states_3 = [STATE_WAITING, STATE_SKIPPED]

        sim.add_task_states('1', states_1)
        sim.add_task_states('2', states_2)
        sim.add_task_states('3', states_3)

        # Test
        task_list = sim.get_all_tasks().response_body
        completed_tasks = self.command.poll(task_list, {})

        # Verify
        self.assertTrue(isinstance(completed_tasks, list))
        self.assertEqual(2, len(completed_tasks))

        expected_tags = [
            'abort',
            'header',
            'delayed-spinner',
            'running-spinner',
            'succeeded',  # states_1
            'header',
            'delayed-spinner',
            'running-spinner',
            'failed'
        ]  # states_2
        self.assertEqual(set(expected_tags), set(self.prompt.get_write_tags()))

    @mock.patch(
        'pulp.client.commands.polling.PollingCommand._render_coded_error')
    def test_failed_task_calls__render_coded_error(self, mock_render_coded):
        """
        When a task fails, it should call _render_coded_error to render the error and suberrors.
        """
        mock_task = mock.MagicMock()
        mock_task.error = {
            'description': 'this is a test of the emergency broadcast system'
        }
        self.command.failed(mock_task)
        mock_render_coded.assert_called_once_with(mock_task.error)

    def test__render_coded_error_called_recursively(self):
        """
        Pass an error with sub_errors, they should all be rendered.
        """
        mock_error = {
            'description': 'this is a test of the emergency broadcast system',
            'sub_errors': [{
                'description': 'beep1'
            }, {
                'description': 'beep2'
            }]
        }

        # Though a context manager would be prefered, this pattern is necessary to run on python 2.4
        old_prompt = self.command.prompt
        try:
            self.command.prompt = mock.MagicMock()
            self.command._render_coded_error(mock_error)
            self.command.prompt.render_failure_message.assert_has_calls([
                mock.call('this is a test of the emergency broadcast system'),
                mock.call('beep1'),
                mock.call('beep2'),
            ])
        finally:
            self.command.prompt = old_prompt

    def test_cancelled_task(self):
        """
        Task Count: 1
        Statuses: Cancelled after 2 in progress polls
        Results: 1 Cancelled
        """

        # Setup
        sim = TaskSimulator()
        sim.install(self.bindings)

        states = [STATE_WAITING, STATE_RUNNING, STATE_RUNNING, STATE_CANCELED]
        sim.add_task_states('1', states)

        # Test
        task_list = sim.get_all_tasks().response_body
        completed_tasks = self.command.poll(task_list, {})

        # Verify
        self.assertTrue(isinstance(completed_tasks, list))
        self.assertEqual(1, len(completed_tasks))

        expected_tags = [
            'abort', 'delayed-spinner', 'running-spinner', 'running-spinner',
            'running-spinner', 'canceled'
        ]
        self.assertEqual(expected_tags, self.prompt.get_write_tags())

    def test_keyboard_interrupt(self):
        # Setup
        mock_poll_call = mock.MagicMock()._poll_task
        mock_poll_call.side_effect = KeyboardInterrupt()
        self.command._poll_task = mock_poll_call

        sim = TaskSimulator()
        sim.install(self.bindings)

        sim.add_task_state('1', STATE_WAITING)

        # Test
        task_list = sim.get_all_tasks().response_body
        result = self.command.poll(task_list, {})

        # Verify
        self.assertEqual(result, RESULT_ABORTED)

        self.assertEqual(['abort'], self.prompt.get_write_tags())