Пример #1
0
    def test_foreach_controller_should_handle_errors_in_individual_controllers(
            self):
        successful_controllers = []

        def controller_operation(controller):
            if controller == 'cpu':
                raise Exception('A test exception')

            successful_controllers.append(controller)

        with patch("azurelinuxagent.common.cgroupapi.logger.warn"
                   ) as mock_logger_warn:
            CGroupsApi._foreach_controller(controller_operation,
                                           'A dummy message')

            self.assertIn(
                'memory', successful_controllers,
                'The operation was not executed on the memory controller')
            self.assertEqual(
                len(successful_controllers), 1,
                'The operation was not executed on unexpected controllers: {0}'
                .format(successful_controllers))

            args, kwargs = mock_logger_warn.call_args
            (message_format, controller, error, message) = args
            self.assertEquals(message_format,
                              'Error in cgroup controller "{0}": {1}. {2}')
            self.assertEquals(controller, 'cpu')
            self.assertEquals(error, 'A test exception')
            self.assertEquals(message, 'A dummy message')
Пример #2
0
    def test_foreach_controller_should_execute_operation_on_all_mounted_controllers(
            self):
        executed_controllers = []

        def controller_operation(controller):
            executed_controllers.append(controller)

        CGroupsApi._foreach_controller(controller_operation, 'A dummy message')

        # The setUp method mocks azurelinuxagent.common.cgroupapi.CGROUPS_FILE_SYSTEM_ROOT to have the cpu and memory controllers mounted
        self.assertIn('cpu', executed_controllers,
                      'The operation was not executed on the cpu controller')
        self.assertIn(
            'memory', executed_controllers,
            'The operation was not executed on the memory controller')
        self.assertEqual(
            len(executed_controllers), 2,
            'The operation was not executed on unexpected controllers: {0}'.
            format(executed_controllers))