コード例 #1
0
ファイル: task_runner_test.py プロジェクト: pombreda/luci-py
 def test_run_command_os_error(self):
     # This runs the command for real.
     # OS specific error, fix expectation for other OSes.
     output = ('Command "executable_that_shouldnt_be_on_your_system '
               'thus_raising_OSError" failed to start.\n'
               'Error: [Error 2] The system cannot find the file specified'
               ) if sys.platform == 'win32' else (
                   'Command "executable_that_shouldnt_be_on_your_system '
                   'thus_raising_OSError" failed to start.\n'
                   'Error: [Errno 2] No such file or directory')
     self.requests(cost_usd=10., exit_code=1, output=output)
     task_details = task_runner.TaskDetails({
         'bot_id':
         'localhost',
         'command': [
             'executable_that_shouldnt_be_on_your_system',
             'thus_raising_OSError',
         ],
         'data': [],
         'env': {},
         'grace_period':
         30.,
         'hard_timeout':
         6,
         'io_timeout':
         6,
         'task_id':
         23,
     })
     self.assertEqual(1, self._run_command(task_details))
コード例 #2
0
 def test_run_command_os_error(self):
   self.requests(
       cost_usd=10.,
       exit_code=1,
       output_re=(
           # This is a beginning of run_isolate.py's output if binary is not
           # found.
           r'^<The executable does not exist or a dependent library is '
           r'missing>'))
   task_details = task_runner.TaskDetails(
       {
         'bot_id': 'localhost',
         'command': [
           'executable_that_shouldnt_be_on_your_system',
           'thus_raising_OSError',
         ],
         'env': {},
         'env_prefixes': {},
         'extra_args': [],
         'grace_period': 30.,
         'hard_timeout': 6,
         'io_timeout': 6,
         'isolated': None,
         'task_id': 23,
       })
   expected = {
     u'exit_code': 1,
     u'hard_timeout': False,
     u'io_timeout': False,
     u'must_signal_internal_failure': None,
     u'version': task_runner.OUT_VERSION,
   }
   self.assertEqual(expected, self._run_command(task_details))
コード例 #3
0
 def get_task_details(cls, *args, **kwargs):
     return task_runner.TaskDetails(get_manifest(*args, **kwargs))
コード例 #4
0
    def test_run_command_large(self):
        # Method should have "self" as first argument - pylint: disable=E0213
        class Popen(object):
            """Mocks the process so we can control how data is returned."""
            def __init__(self2, cmd, cwd, env, stdout, stderr, stdin,
                         detached):
                self.assertEqual(task_details.command, cmd)
                self.assertEqual(self.work_dir, cwd)
                expected_env = os.environ.copy()
                expected_env['foo'] = 'bar'
                self.assertEqual(expected_env, env)
                self.assertEqual(subprocess42.PIPE, stdout)
                self.assertEqual(subprocess42.STDOUT, stderr)
                self.assertEqual(subprocess42.PIPE, stdin)
                self.assertEqual(True, detached)
                self2._out = [
                    'hi!\n',
                    'hi!\n',
                    'hi!\n' * 100000,
                    'hi!\n',
                ]

            def yield_any(self2, maxsize, soft_timeout):
                self.assertLess(0, maxsize)
                self.assertLess(0, soft_timeout)
                for i in self2._out:
                    yield 'stdout', i

            @staticmethod
            def wait():
                return 0

            @staticmethod
            def kill():
                self.fail()

        self.mock(subprocess42, 'Popen', Popen)

        def check_final(kwargs):
            self.assertEqual(
                {
                    'data': {
                        # That's because the cost includes the duration starting at start,
                        # not when the process was started.
                        'cost_usd': 10.,
                        'duration': 0.,
                        'exit_code': 0,
                        'hard_timeout': False,
                        'id': 'localhost',
                        'io_timeout': False,
                        'output': base64.b64encode('hi!\n'),
                        'output_chunk_start': 100002 * 4,
                        'task_id': 23,
                    },
                    'headers': {
                        'X-XSRF-Token': 'token'
                    },
                },
                kwargs)

        requests = [
            (
                'https://localhost:1/auth/api/v1/accounts/self/xsrf_token',
                {
                    'data': {},
                    'headers': {
                        'X-XSRF-Token-Request': '1'
                    }
                },
                {
                    'xsrf_token': 'token'
                },
            ),
            (
                'https://localhost:1/swarming/api/v1/bot/task_update/23',
                {
                    'data': {
                        'cost_usd': 10.,
                        'id': 'localhost',
                        'task_id': 23,
                    },
                    'headers': {
                        'X-XSRF-Token': 'token'
                    },
                },
                {},
            ),
            (
                'https://localhost:1/swarming/api/v1/bot/task_update/23',
                {
                    'data': {
                        'cost_usd': 10.,
                        'id': 'localhost',
                        'output': base64.b64encode('hi!\n' * 100002),
                        'output_chunk_start': 0,
                        'task_id': 23,
                    },
                    'headers': {
                        'X-XSRF-Token': 'token'
                    },
                },
                {},
            ),
            (
                'https://localhost:1/swarming/api/v1/bot/task_update/23',
                check_final,
                {},
            ),
        ]
        self.expected_requests(requests)
        task_details = task_runner.TaskDetails({
            'bot_id':
            'localhost',
            'command': ['large', 'executable'],
            'data': [],
            'env': {
                'foo': 'bar'
            },
            'extra_args': [],
            'grace_period':
            30.,
            'hard_timeout':
            60,
            'inputs_ref':
            None,
            'io_timeout':
            60,
            'task_id':
            23,
        })
        expected = {
            u'exit_code': 0,
            u'hard_timeout': False,
            u'io_timeout': False,
            u'must_signal_internal_failure': None,
            u'version': task_runner.OUT_VERSION,
        }
        self.assertEqual(expected, self._run_command(task_details))
コード例 #5
0
  def test_run_command_large(self):
    # Method should have "self" as first argument - pylint: disable=E0213
    class Popen(object):
      """Mocks the process so we can control how data is returned."""
      def __init__(self2, _cmd, cwd, env, stdout, stderr, stdin, detached):
        self.assertEqual(self.work_dir, cwd)
        expected_env = os.environ.copy()
        # In particular, foo=bar is not set here, it will be passed to
        # run_isolated as an argument.
        expected_env['LUCI_CONTEXT'] = env['LUCI_CONTEXT']  # tmp file
        self.assertEqual(expected_env, env)
        self.assertEqual(subprocess42.PIPE, stdout)
        self.assertEqual(subprocess42.STDOUT, stderr)
        self.assertEqual(subprocess42.PIPE, stdin)
        self.assertEqual(True, detached)
        self2._out = [
          'hi!\n',
          'hi!\n',
          'hi!\n' * 100000,
          'hi!\n',
        ]

      def yield_any(self2, maxsize, timeout):
        self.assertLess(0, maxsize)
        self.assertLess(0, timeout)
        for i in self2._out:
          yield 'stdout', i

      @staticmethod
      def wait():
        return 0

      @staticmethod
      def kill():
        self.fail()

    self.mock(subprocess42, 'Popen', Popen)

    def check_final(kwargs):
      self.assertEqual(
          {
            'data': {
              # That's because the cost includes the duration starting at start,
              # not when the process was started.
              'cost_usd': 10.,
              'duration': 0.,
              'exit_code': 0,
              'hard_timeout': False,
              'id': 'localhost',
              'io_timeout': False,
              'output': base64.b64encode('hi!\n'),
              'output_chunk_start': 100002*4,
              'task_id': 23,
            },
            'follow_redirects': False,
            'timeout': 180,
            'headers': {'Cookie': 'GOOGAPPUID=42'},
          },
          kwargs)

    requests = [
      (
        'https://localhost:1/swarming/api/v1/bot/task_update/23',
        {
          'data': {
            'cost_usd': 10.,
            'id': 'localhost',
            'task_id': 23,
          },
          'follow_redirects': False,
          'timeout': 180,
          'headers': {'Cookie': 'GOOGAPPUID=42'},
        },
        {'must_stop': False, 'ok': True},
      ),
      (
        'https://localhost:1/swarming/api/v1/bot/task_update/23',
        {
          'data': {
            'cost_usd': 10.,
            'id': 'localhost',
            'output': base64.b64encode('hi!\n' * 100002),
            'output_chunk_start': 0,
            'task_id': 23,
          },
          'follow_redirects': False,
          'timeout': 180,
          'headers': {'Cookie': 'GOOGAPPUID=42'},
        },
        {'must_stop': False, 'ok': True},
      ),
      (
        'https://localhost:1/swarming/api/v1/bot/task_update/23',
        check_final,
        {'must_stop': False, 'ok': True},
      ),
    ]
    self.expected_requests(requests)
    task_details = task_runner.TaskDetails(
        {
          'bot_id': 'localhost',
          'command': ['large', 'executable'],
          'env': {'foo': 'bar'},
          'env_prefixes': {},
          'extra_args': [],
          'grace_period': 30.,
          'hard_timeout': 60,
          'io_timeout': 60,
          'isolated': None,
          'task_id': 23,
        })
    expected = {
      u'exit_code': 0,
      u'hard_timeout': False,
      u'io_timeout': False,
      u'must_signal_internal_failure': None,
      u'version': task_runner.OUT_VERSION,
    }
    self.assertEqual(expected, self._run_command(task_details))