Ejemplo n.º 1
0
 def _run_command(self, task_details):
     start = time.time()
     self.mock(time, 'time', lambda: start + 10)
     server = xsrf_client.XsrfRemote('https://localhost:1/')
     return task_runner.run_command(
         server, task_details, '.', 3600., start,
         os.path.join(self.work_dir, 'task_summary.json'))
Ejemplo n.º 2
0
 def _run_command(self, task_details):
   start = time.time()
   self.mock(time, 'time', lambda: start + 10)
   server = xsrf_client.XsrfRemote('https://localhost:1/')
   return task_runner.run_command(
       server, task_details, '.', 3600., start,
       os.path.join(self.work_dir, 'task_summary.json'))
Ejemplo n.º 3
0
 def _run_command(self, task_details, headers_cb=None):
   start = time.time()
   self.mock(time, 'time', lambda: start + 10)
   remote = remote_client.createRemoteClient('https://localhost:1', headers_cb)
   return task_runner.run_command(
       remote, task_details, self.work_dir, 3600.,
       start, 1, '/path/to/file')
Ejemplo n.º 4
0
def run_command(server_url, work_dir, task_details, headers_cb):
  """Runs a command with an initialized client."""
  remote = remote_client.createRemoteClient(
      server_url, headers_cb, 'localhost', work_dir, False)
  with luci_context.stage(local_auth=None) as ctx_file:
    return task_runner.run_command(
        remote, task_details, work_dir, 3600.,
        time.time(), ['--min-free-space', '1'], '/path/to/file', ctx_file)
Ejemplo n.º 5
0
 def _run_command(self, task_details):
   # Dot not mock time since this test class is testing timeouts.
   remote = remote_client.createRemoteClient('https://localhost:1', None,
                                             'localhost', self.work_dir,
                                             False)
   with luci_context.stage(local_auth=None) as ctx_file:
     return task_runner.run_command(
         remote, task_details, self.work_dir, 3600., time.time(),
         ['--min-free-space', '1'], '/path/to/file', ctx_file)
Ejemplo n.º 6
0
 def _run_command(self, task_details, headers_cb=None):
   start = time.time()
   self.mock(time, 'time', lambda: start + 10)
   remote = remote_client.createRemoteClient('https://localhost:1', headers_cb,
                                             'localhost', self.work_dir,
                                             False)
   with luci_context.stage(local_auth=None) as ctx_file:
     return task_runner.run_command(
         remote, task_details, self.work_dir, 3600.,
         start, ['--min-free-space', '1'], '/path/to/file', ctx_file)
Ejemplo n.º 7
0
 def _run_command(self, task_details):
     # Dot not mock time since this test class is testing timeouts.
     server = xsrf_client.XsrfRemote('https://localhost:1/')
     return task_runner.run_command(server, task_details, self.work_dir,
                                    3600., time.time())
Ejemplo n.º 8
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('./', cwd)
                expected_env = os.environ.copy()
                expected_env['foo'] = 'bar'
                self.assertEqual(expected_env, env)
                self.assertEqual(subprocess.PIPE, stdout)
                self.assertEqual(subprocess.STDOUT, stderr)
                self.assertEqual(subprocess.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)
        server = xsrf_client.XsrfRemote('https://localhost:1/')
        task_details = task_runner.TaskDetails({
            'bot_id':
            'localhost',
            'command': ['large', 'executable'],
            'data': [],
            'env': {
                'foo': 'bar'
            },
            'grace_period':
            30.,
            'hard_timeout':
            60,
            'io_timeout':
            60,
            'task_id':
            23,
        })
        start = time.time()
        self.mock(time, 'time', lambda: start + 10)
        r = task_runner.run_command(
            server, task_details, './', 3600., start,
            os.path.join(self.work_dir, 'task_summary.json'))
        self.assertEqual(0, r)
Ejemplo n.º 9
0
 def _run_command(self, task_details):
   # Dot not mock time since this test class is testing timeouts.
   server = xsrf_client.XsrfRemote('https://localhost:1/')
   return task_runner.run_command(
       server, task_details, self.work_dir, 3600., time.time())
Ejemplo n.º 10
0
 def _run_command(self, task_details):
   # Dot not mock time since this test class is testing timeouts.
   server = 'https://localhost:1'
   return task_runner.run_command(
       server, task_details, self.work_dir, 3600., time.time(), 1)
Ejemplo n.º 11
0
 def _run_command(self, task_details):
   start = time.time()
   self.mock(time, 'time', lambda: start + 10)
   server = 'https://localhost:1'
   return task_runner.run_command(
       server, task_details, self.work_dir, 3600., start, 1)
Ejemplo n.º 12
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('./', cwd)
        expected_env = os.environ.copy()
        expected_env['foo'] = 'bar'
        self.assertEqual(expected_env, env)
        self.assertEqual(subprocess.PIPE, stdout)
        self.assertEqual(subprocess.STDOUT, stderr)
        self.assertEqual(subprocess.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)
    server = xsrf_client.XsrfRemote('https://localhost:1/')
    task_details = task_runner.TaskDetails(
        {
          'bot_id': 'localhost',
          'command': ['large', 'executable'],
          'data': [],
          'env': {'foo': 'bar'},
          'grace_period': 30.,
          'hard_timeout': 60,
          'io_timeout': 60,
          'task_id': 23,
        })
    start = time.time()
    self.mock(time, 'time', lambda: start + 10)
    r = task_runner.run_command(
        server, task_details, './', 3600., start,
        os.path.join(self.work_dir, 'task_summary.json'))
    self.assertEqual(0, r)
Ejemplo n.º 13
0
 def _run_command(self, task_details):
   # Dot not mock time since this test class is testing timeouts.
   remote = remote_client.createRemoteClient('https://localhost:1', None)
   return task_runner.run_command(
       remote, task_details, self.work_dir, 3600., time.time(), 1,
       '/path/to/file')
Ejemplo n.º 14
0
 def _run_command(self, task_details):
   start = time.time()
   self.mock(time, 'time', lambda: start + 10)
   server = xsrf_client.XsrfRemote('https://localhost:1/')
   return task_runner.run_command(
       server, task_details, '.', 3600., start)