Пример #1
0
    def test_run_bot(self):
        # Test the run_bot() loop. Does not use self.bot.
        self.mock(time, "time", lambda: 126.0)

        class Foo(Exception):
            pass

        def poll_server(botobj, _):
            sleep_streak = botobj.state["sleep_streak"]
            self.assertEqual(botobj.remote, self.server)
            if sleep_streak == 5:
                raise Exception("Jumping out of the loop")
            return False

        self.mock(bot_main, "poll_server", poll_server)

        def post_error(botobj, e):
            self.assertEqual(self.server, botobj._remote)
            lines = e.splitlines()
            self.assertEqual("Jumping out of the loop", lines[0])
            self.assertEqual("Traceback (most recent call last):", lines[1])
            raise Foo("Necessary to get out of the loop")

        self.mock(bot.Bot, "post_error", post_error)

        self.mock(bot_main, "get_remote", lambda: self.server)

        self.expected_requests([("https://localhost:1/swarming/api/v1/bot/server_ping", {}, "foo", None)])

        with self.assertRaises(Foo):
            bot_main.run_bot(None)
        self.assertEqual(os_utilities.get_hostname_short(), os.environ["SWARMING_BOT_ID"])
Пример #2
0
    def test_run_bot(self):
        # Test the run_bot() loop. Does not use self.bot.
        self.mock(time, 'time', lambda: 126.0)

        class Foo(Exception):
            pass

        def poll_server(botobj, _):
            sleep_streak = botobj.state['sleep_streak']
            self.assertEqual(botobj.remote, self.server)
            if sleep_streak == 5:
                raise Exception('Jumping out of the loop')
            return False

        self.mock(bot_main, 'poll_server', poll_server)

        def post_error(botobj, e):
            self.assertEqual(self.server, botobj._remote)
            lines = e.splitlines()
            self.assertEqual('Jumping out of the loop', lines[0])
            self.assertEqual('Traceback (most recent call last):', lines[1])
            raise Foo('Necessary to get out of the loop')

        self.mock(bot.Bot, 'post_error', post_error)

        self.mock(bot_main, 'get_remote', lambda: self.server)

        self.expected_requests([
            (
                'https://localhost:1/swarming/api/v1/bot/server_ping',
                {},
                'foo',
                None,
            ),
        ])

        with self.assertRaises(Foo):
            bot_main.run_bot(None)
        self.assertEqual(os_utilities.get_hostname_short(),
                         os.environ['SWARMING_BOT_ID'])
Пример #3
0
  def test_run_bot(self):
    # Test the run_bot() loop. Does not use self.bot.
    self.mock(time, 'time', lambda: 126.0)
    class Foo(Exception):
      pass

    def poll_server(botobj):
      sleep_streak = botobj.state['sleep_streak']
      self.assertEqual(botobj.remote, self.server)
      if sleep_streak == 5:
        raise Exception('Jumping out of the loop')
      return False
    self.mock(bot_main, 'poll_server', poll_server)

    def post_error(botobj, e):
      self.assertEqual(self.server, botobj._remote)
      lines = e.splitlines()
      self.assertEqual('Jumping out of the loop', lines[0])
      self.assertEqual('Traceback (most recent call last):', lines[1])
      raise Foo('Necessary to get out of the loop')
    self.mock(bot.Bot, 'post_error', post_error)

    self.mock(bot_main, 'get_remote', lambda: self.server)

    self.expected_requests(
        [
          (
            'https://localhost:1/swarming/api/v1/bot/server_ping',
            {}, 'foo', None,
          ),
        ])

    with self.assertRaises(Foo):
      bot_main.run_bot(None)
    self.assertEqual(
        os_utilities.get_hostname_short(), os.environ['SWARMING_BOT_ID'])
Пример #4
0
  def test_run_bot(self):
    # Test the run_bot() loop. Does not use self.bot.
    self.mock(time, 'time', lambda: 126.0)
    class Foo(Exception):
      pass

    def poll_server(botobj, _):
      sleep_streak = botobj.state['sleep_streak']
      self.assertEqual(self.url, botobj.server)
      if sleep_streak == 5:
        raise Exception('Jumping out of the loop')
      return False
    self.mock(bot_main, 'poll_server', poll_server)

    def post_error(botobj, e):
      self.assertEqual(self.url, botobj.server)
      lines = e.splitlines()
      self.assertEqual('Jumping out of the loop', lines[0])
      self.assertEqual('Traceback (most recent call last):', lines[1])
      raise Foo('Necessary to get out of the loop')
    self.mock(bot.Bot, 'post_error', post_error)

    self.mock(
        bot_main, 'get_config',
        lambda: {'server': self.url, 'server_version': '1'})
    self.mock(
        bot_main, 'get_dimensions', lambda _: self.attributes['dimensions'])
    self.mock(os_utilities, 'get_state', lambda *_: self.attributes['state'])

    # Method should have "self" as first argument - pylint: disable=E0213
    # pylint: disable=unused-argument
    class Popen(object):
      def __init__(
          self2, cmd, detached, cwd, stdout, stderr, stdin, close_fds):
        self2.returncode = None
        expected = [sys.executable, bot_main.THIS_FILE, 'run_isolated']
        self.assertEqual(expected, cmd[:len(expected)])
        self.assertEqual(True, detached)
        self.assertEqual(subprocess42.PIPE, stdout)
        self.assertEqual(subprocess42.STDOUT, stderr)
        self.assertEqual(subprocess42.PIPE, stdin)
        self.assertEqual(sys.platform != 'win32', close_fds)

      def communicate(self2, i):
        self.assertEqual(None, i)
        self2.returncode = 0
        return '', None
    self.mock(subprocess42, 'Popen', Popen)

    self.expected_requests(
        [
          (
            'https://localhost:1/swarming/api/v1/bot/server_ping',
            {}, 'foo', None,
          ),
          (
            'https://localhost:1/swarming/api/v1/bot/handshake',
            {'data': self.attributes},
            {'bot_version': '123', 'server': self.url, 'server_version': 1},
          ),
        ])

    with self.assertRaises(Foo):
      bot_main.run_bot(None)
    self.assertEqual(
        self.attributes['dimensions']['id'][0], os.environ['SWARMING_BOT_ID'])
Пример #5
0
  def test_run_bot(self):
    self.mock(threading, 'Event', FakeThreadingEvent)

    # Test the run_bot() loop. Does not use self.bot.
    self.mock(time, 'time', lambda: 126.0)
    class Foo(Exception):
      pass

    def poll_server(botobj, _quit_bit, _last_action):
      sleep_streak = botobj.state['sleep_streak']
      self.assertEqual(self.url, botobj.server)
      if sleep_streak == 5:
        raise Exception('Jumping out of the loop')
      return False
    self.mock(bot_main, 'poll_server', poll_server)

    def post_error(botobj, e):
      self.assertEqual(self.url, botobj.server)
      lines = e.splitlines()
      self.assertEqual('Jumping out of the loop', lines[0])
      self.assertEqual('Traceback (most recent call last):', lines[1])
      raise Foo('Necessary to get out of the loop')
    self.mock(bot.Bot, 'post_error', post_error)

    orig = bot_main.get_bot
    botobj = [None]
    def get_bot():
      botobj[0] = orig()
      return botobj[0]
    self.mock(bot_main, 'get_bot', get_bot)

    self.mock(
        bot_main, 'get_config',
        lambda: {'server': self.url, 'server_version': '1'})
    self.mock(
        bot_main, 'get_dimensions', lambda _: self.attributes['dimensions'])
    self.mock(os_utilities, 'get_state', lambda *_: self.attributes['state'])

    # Method should have "self" as first argument - pylint: disable=E0213
    # pylint: disable=unused-argument
    class Popen(object):
      def __init__(
          self2, cmd, detached, cwd, stdout, stderr, stdin, close_fds):
        self2.returncode = None
        expected = [sys.executable, bot_main.THIS_FILE, 'run_isolated']
        self.assertEqual(expected, cmd[:len(expected)])
        self.assertEqual(True, detached)
        self.assertEqual(subprocess42.PIPE, stdout)
        self.assertEqual(subprocess42.STDOUT, stderr)
        self.assertEqual(subprocess42.PIPE, stdin)
        self.assertEqual(sys.platform != 'win32', close_fds)

      def communicate(self2, i):
        self.assertEqual(None, i)
        self2.returncode = 0
        return '', None
    self.mock(subprocess42, 'Popen', Popen)

    self.expected_requests(
        [
          (
            'https://localhost:1/swarming/api/v1/bot/server_ping',
            {}, 'foo', None,
          ),
          (
            'https://localhost:1/swarming/api/v1/bot/handshake',
            {
              'data': self.attributes,
              'follow_redirects': False,
              'headers': {},
              'timeout': remote_client.NET_CONNECTION_TIMEOUT_SEC,
            },
            None, # fails, gets retried
          ),
          (
            'https://localhost:1/swarming/api/v1/bot/handshake',
            {
              'data': self.attributes,
              'follow_redirects': False,
              'headers': {},
              'timeout': remote_client.NET_CONNECTION_TIMEOUT_SEC,
            },
            {
              'bot_version': '123',
              'server': self.url,
              'server_version': 1,
              'bot_group_cfg_version': 'abc:def',
              'bot_group_cfg': {
                'dimensions': {'bot_side': ['A']},
              },
            },
          ),
        ])

    with self.assertRaises(Foo):
      bot_main.run_bot(None)
    self.assertEqual(
        self.attributes['dimensions']['id'][0], os.environ['SWARMING_BOT_ID'])

    self.assertEqual({
      'bot_side': ['A'],
      'foo': ['bar'],
      'id': ['localhost'],
      'pool': ['default'],
    }, botobj[0].dimensions)
Пример #6
0
    def test_run_bot(self):
        # Test the run_bot() loop. Does not use self.bot.
        self.mock(time, 'time', lambda: 126.0)

        class Foo(Exception):
            pass

        def poll_server(botobj, _):
            sleep_streak = botobj.state['sleep_streak']
            self.assertEqual(self.url, botobj.server)
            if sleep_streak == 5:
                raise Exception('Jumping out of the loop')
            return False

        self.mock(bot_main, 'poll_server', poll_server)

        def post_error(botobj, e):
            self.assertEqual(self.url, botobj.server)
            lines = e.splitlines()
            self.assertEqual('Jumping out of the loop', lines[0])
            self.assertEqual('Traceback (most recent call last):', lines[1])
            raise Foo('Necessary to get out of the loop')

        self.mock(bot.Bot, 'post_error', post_error)

        self.mock(bot_main, 'get_config', lambda: {
            'server': self.url,
            'server_version': '1'
        })
        self.mock(bot_main, 'get_dimensions',
                  lambda _: self.attributes['dimensions'])
        self.mock(os_utilities, 'get_state',
                  lambda *_: self.attributes['state'])

        # Method should have "self" as first argument - pylint: disable=E0213
        # pylint: disable=unused-argument
        class Popen(object):
            def __init__(self2, cmd, detached, cwd, stdout, stderr, stdin,
                         close_fds):
                self2.returncode = None
                expected = [sys.executable, bot_main.THIS_FILE, 'run_isolated']
                self.assertEqual(expected, cmd[:len(expected)])
                self.assertEqual(True, detached)
                self.assertEqual(subprocess42.PIPE, stdout)
                self.assertEqual(subprocess42.STDOUT, stderr)
                self.assertEqual(subprocess42.PIPE, stdin)
                self.assertEqual(sys.platform != 'win32', close_fds)

            def communicate(self2, i):
                self.assertEqual(None, i)
                self2.returncode = 0
                return '', None

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

        self.expected_requests([
            (
                'https://localhost:1/swarming/api/v1/bot/server_ping',
                {},
                'foo',
                None,
            ),
            (
                'https://localhost:1/swarming/api/v1/bot/handshake',
                {
                    'data': self.attributes
                },
                {
                    'bot_version': '123',
                    'server': self.url,
                    'server_version': 1
                },
            ),
        ])

        with self.assertRaises(Foo):
            bot_main.run_bot(None)
        self.assertEqual(self.attributes['dimensions']['id'][0],
                         os.environ['SWARMING_BOT_ID'])