Esempio n. 1
0
    def test_activate_objects_with_ignored_error(self):
        connection = MagicMock()

        args = self.parse_args('activate', '--ignore-errors', 'myname', 'anothername')

        message_builder = MessageBuilder()

        response_iter = iter([(message_builder.build_results_without_messages(), None),
                              (message_builder.build_results_with_errors(), None)])

        with patch('sap.adt.wb.try_activate') as fake_activate, \
             patch_get_print_console_with_buffer() as fake_console:
            fake_activate.side_effect = lambda x: next(response_iter)

            exit_code = args.execute(connection, args)
            self.assertEqual(exit_code, 1)

        self.assertEqual(fake_console.return_value.std_output.getvalue(), f'''Activating 2 objects:
* myname (1/2)
* anothername (2/2)
{message_builder.error_message[1]}Activation has finished
Warnings: 0
Errors: 1
Inactive objects:
  FAKE anothername
''')
 def setUp(self):
     self.worker = sap.cli.wb.ObjectActivationWorker()
     self.activated_object = Mock()
     self.message_builder = MessageBuilder()
     self.stats = sap.cli.wb.ObjectActivationStats()
class TestObjectActivationWorker(unittest.TestCase):
    def setUp(self):
        self.worker = sap.cli.wb.ObjectActivationWorker()
        self.activated_object = Mock()
        self.message_builder = MessageBuilder()
        self.stats = sap.cli.wb.ObjectActivationStats()

    def test_properties(self):
        self.assertFalse(self.worker.continue_on_errors)
        self.assertFalse(self.worker.warnings_as_errors)

        self.worker.continue_on_errors = True
        self.assertTrue(self.worker.continue_on_errors)

        self.worker.warnings_as_errors = True
        self.assertTrue(self.worker.warnings_as_errors)

    def test_begin(self):
        cases = [(None, 'Activating:\n'), (1, 'Activating:\n'),
                 (2, 'Activating 2 objects:\n')]

        for no, case in enumerate(cases):
            with patch_get_print_console_with_buffer() as fake_console:
                self.worker.begin(case[0])

            self.assertEqual(fake_console.return_value.err_output.getvalue(),
                             '', f'Case {no}')
            self.assertEqual(fake_console.return_value.std_output.getvalue(),
                             case[1], f'Case {no}')

    def test_start_object(self):
        cases = [('CL_NONE', 1, None, '* CL_NONE\n'),
                 ('CL_ONE', 1, 1, '* CL_ONE\n'),
                 ('CL_TWO', 1, 2, '* CL_TWO (1/2)\n')]

        for no, case in enumerate(cases):
            with patch_get_print_console_with_buffer() as fake_console:
                self.worker.start_object(case[0], case[1], case[2])

            self.assertEqual(fake_console.return_value.err_output.getvalue(),
                             '', f'Case {no}')
            self.assertEqual(fake_console.return_value.std_output.getvalue(),
                             case[3], f'Case {no}')

    def test_handle_message(self):
        message, lines = self.message_builder.build_warning()

        with patch_get_print_console_with_buffer() as fake_console:
            self.worker.handle_message(message)

        self.assertEqual(fake_console.return_value.err_output.getvalue(), '')
        self.assertEqual(fake_console.return_value.std_output.getvalue(),
                         lines)

    def assert_ok_stats(self, stats):
        self.assertEqual(stats.warnings, 0)
        self.assertEqual(stats.errors, 0)
        self.assertEqual(stats.active_objects, [self.activated_object])
        self.assertEqual(stats.inactive_objects, [])

    def test_handle_results_without_messages(self):
        with patch_get_print_console_with_buffer() as fake_console:
            self.worker.handle_results(
                'CL_NO_MESSAGES', self.activated_object,
                self.message_builder.build_results_without_messages(),
                self.stats)

        self.assertEqual(fake_console.return_value.err_output.getvalue(), '')
        self.assertEqual(fake_console.return_value.std_output.getvalue(), '')

        self.assert_ok_stats(self.stats)

    def assert_error_stats(self, stats):
        self.assertEqual(stats.warnings, 0)
        self.assertEqual(stats.errors, 1)
        self.assertEqual(stats.active_objects, [])
        self.assertEqual(stats.inactive_objects, [self.activated_object])

    def assert_error_message_output(self, fake_console):
        self.assertEqual(fake_console.return_value.err_output.getvalue(), '')
        self.assertEqual(fake_console.return_value.std_output.getvalue(),
                         self.message_builder.error_message[1])

    def test_handle_results_with_errors_no_continue(self):
        with self.assertRaises(sap.cli.wb.StopObjectActivation) as caught, \
             patch_get_print_console_with_buffer() as fake_console:
            self.worker.handle_results(
                'CL_NO_MESSAGES', self.activated_object,
                self.message_builder.build_results_with_errors(), self.stats)

        self.assert_error_message_output(fake_console)
        self.assert_error_stats(caught.exception.stats)

    def test_handle_results_with_errors_continue(self):
        self.worker.continue_on_errors = True

        with patch_get_print_console_with_buffer() as fake_console:
            stats = self.worker.handle_results(
                'CL_NO_MESSAGES', self.activated_object,
                self.message_builder.build_results_with_errors(), self.stats)

        self.assert_error_message_output(fake_console)
        self.assert_error_stats(self.stats)

    def assert_warning_stats(self, stats):
        self.assertEqual(stats.warnings, 1)
        self.assertEqual(stats.active_objects, [self.activated_object])
        self.assertEqual(stats.inactive_objects, [])

    def assert_warning_message_output(self, fake_console):
        self.assertEqual(fake_console.return_value.err_output.getvalue(), '')
        self.assertEqual(fake_console.return_value.std_output.getvalue(),
                         self.message_builder.warning_message[1])

    def test_handle_results_with_warnings_and_stop(self):
        self.worker.warnings_as_errors = True

        with self.assertRaises(sap.cli.wb.StopObjectActivation) as caught, \
             patch_get_print_console_with_buffer() as fake_console:
            self.worker.handle_results(
                'CL_NO_MESSAGES', self.activated_object,
                self.message_builder.build_results_with_warnings(), self.stats)

        self.assert_warning_message_output(fake_console)
        self.assert_warning_stats(caught.exception.stats)
        self.assertEqual(caught.exception.stats.errors, 1)

    def test_handle_results_with_warnings(self):
        with patch_get_print_console_with_buffer() as fake_console:
            stats = self.worker.handle_results(
                'CL_NO_MESSAGES', self.activated_object,
                self.message_builder.build_results_with_warnings(), self.stats)

        self.assert_warning_message_output(fake_console)
        self.assert_warning_stats(self.stats)
        self.assertEqual(self.stats.errors, 0)

    def test_handle_results_with_warnings_as_error_and_ignore(self):
        self.worker.continue_on_errors = True
        self.worker.warnings_as_errors = True

        with patch_get_print_console_with_buffer() as fake_console:
            stats = self.worker.handle_results(
                'CL_NO_MESSAGES', self.activated_object,
                self.message_builder.build_results_with_warnings(), self.stats)

        self.assert_warning_message_output(fake_console)
        self.assert_warning_stats(self.stats)
        self.assertEqual(self.stats.errors, 1)

    def test_activate_sequentially(self):
        with patch('sap.adt.wb.try_activate') as fake_try_activate, \
             patch_get_print_console_with_buffer() as fake_console:

            fake_try_activate.return_value = (
                self.message_builder.build_results_without_messages(), None)
            items = (itm for itm in [('mock_obj_name', self.activated_object)])
            stats = self.worker.activate_sequentially(items, count=2)

        self.assertEqual(fake_console.return_value.err_output.getvalue(), '')
        self.assertEqual(fake_console.return_value.std_output.getvalue(),
                         '''Activating 2 objects:
* mock_obj_name (1/2)
''')

        self.assert_ok_stats(stats)