Example #1
0
 def test_with_nonexisting_credentials(self):
   cache = {'service_account_creds':
            os.path.join(DATA_DIR, 'no-such-file-8531.json'),
            'service_accounts_creds_root': 'whatever.the/other/is/absolute'}
   # things should work.
   r = router._HttpRouter(cache, 'https://any.where', dry_run=True)
   self.assertIsInstance(r._http, infra_libs.InstrumentedHttp)
Example #2
0
  def test_invalid_url(self):
    event = LogRequestLite.LogEventLite()
    event.event_time_ms = router.time_ms()
    event.event_code = 1
    event.event_flow_id = 2

    r = router._HttpRouter({}, 'ftp://any.where', dry_run=True)
    self.assertFalse(r.push_event(event))
Example #3
0
    def test_push_smoke(self):
        r = router._HttpRouter({}, 'https://any.where', dry_run=True)

        req = LogRequestLite.LogEventLite()
        req.event_time_ms = router.time_ms()
        req.event_code = 1
        req.event_flow_id = 2
        self.assertTrue(r.push_event(req))
Example #4
0
  def test_push_smoke(self):
    r = router._HttpRouter({}, 'https://any.where', dry_run=True)

    req = LogRequestLite.LogEventLite()
    req.event_time_ms = router.time_ms()
    req.event_code = 1
    req.event_flow_id = 2
    self.assertTrue(r.push_event(req))
Example #5
0
    def test_invalid_url(self):
        event = LogRequestLite.LogEventLite()
        event.event_time_ms = router.time_ms()
        event.event_code = 1
        event.event_flow_id = 2

        r = router._HttpRouter({}, 'ftp://any.where', dry_run=True)
        self.assertFalse(r.push_event(event))
Example #6
0
  def test_push_ok(self):
    # Successfully push event the first time.
    sleep = mock.create_autospec(time.sleep, auto_set=True)
    r = router._HttpRouter({}, 'https://bla.bla', _sleep_fn=sleep)
    r._http = infra_libs.HttpMock([('https://bla.bla', {'status': 200}, '')])

    event = LogRequestLite.LogEventLite()
    event.event_time_ms = router.time_ms()
    event.event_code = 1
    event.event_flow_id = 2
    self.assertTrue(r.push_event(event))
    self.assertEquals(len(sleep.call_args_list), 0)
Example #7
0
  def test_push_fail(self):
    # Fail to push events even after all retries
    sleep = mock.create_autospec(time.sleep, auto_set=True)
    r = router._HttpRouter({}, 'https://bla.bla', _sleep_fn=sleep)
    r._http = infra_libs.HttpMock([('https://bla.bla', {'status': 403}, '')])

    event = LogRequestLite.LogEventLite()
    event.event_time_ms = router.time_ms()
    event.event_code = 1
    event.event_flow_id = 2
    self.assertFalse(r.push_event(event))
    self.assertEquals(len(sleep.call_args_list), r.try_num - 1)
Example #8
0
    def test_logs_success(self, logdebug):
        r = router._HttpRouter({}, 'https://bla.bla')
        r._http = infra_libs.HttpMock([('https://bla.bla', {
            'status': 200
        }, '')])

        event = LogRequestLite.LogEventLite()
        event.event_time_ms = router.time_ms()
        event.event_code = 1
        event.event_flow_id = 2
        self.assertTrue(r.push_event(event))
        self.assertIn(mock.call('Succeeded POSTing data after %d attempts', 1),
                      logdebug.call_args_list)
Example #9
0
    def test_push_fail(self):
        # Fail to push events even after all retries
        sleep = mock.create_autospec(time.sleep, auto_set=True)
        r = router._HttpRouter({}, 'https://bla.bla', _sleep_fn=sleep)
        r._http = infra_libs.HttpMock([('https://bla.bla', {
            'status': 403
        }, '')])

        event = LogRequestLite.LogEventLite()
        event.event_time_ms = router.time_ms()
        event.event_code = 1
        event.event_flow_id = 2
        self.assertFalse(r.push_event(event))
        self.assertEquals(len(sleep.call_args_list), r.try_num)
Example #10
0
    def test_push_ok(self):
        # Successfully push event the first time.
        sleep = mock.create_autospec(time.sleep, auto_set=True)
        r = router._HttpRouter({}, 'https://bla.bla', _sleep_fn=sleep)
        r._http = infra_libs.HttpMock([('https://bla.bla', {
            'status': 200
        }, '')])

        event = LogRequestLite.LogEventLite()
        event.event_time_ms = router.time_ms()
        event.event_code = 1
        event.event_flow_id = 2
        self.assertTrue(r.push_event(event))
        self.assertEquals(len(sleep.call_args_list), 0)
Example #11
0
  def test_push_exception(self):
    # Fail to push events even after all retries
    sleep = mock.create_autospec(time.sleep, auto_set=True)
    r = router._HttpRouter({}, 'https://bla.bla', _sleep_fn=sleep)

    class FakeHttp(object):
      # pylint: disable=unused-argument
      def request(self, *args, **kwargs):
        raise ValueError()

    r._http = FakeHttp()

    event = LogRequestLite.LogEventLite()
    event.event_time_ms = router.time_ms()
    event.event_code = 1
    event.event_flow_id = 2
    self.assertFalse(r.push_event(event))
    self.assertEquals(len(sleep.call_args_list), 2)
Example #12
0
    def test_push_exception(self):
        # Fail to push events even after all retries
        sleep = mock.create_autospec(time.sleep, auto_set=True)
        r = router._HttpRouter({}, 'https://bla.bla', _sleep_fn=sleep)

        class FakeHttp(object):
            # pylint: disable=unused-argument
            def request(self, *args, **kwargs):
                raise ValueError()

        r._http = FakeHttp()

        event = LogRequestLite.LogEventLite()
        event.event_time_ms = router.time_ms()
        event.event_code = 1
        event.event_flow_id = 2
        self.assertFalse(r.push_event(event))
        self.assertEquals(len(sleep.call_args_list), 3)
Example #13
0
 def test_with_credentials_smoke(self):
   cache = {'service_account_creds':
             os.path.join(DATA_DIR, 'valid_creds.json'),
            'service_accounts_creds_root': 'whatever.the/other/is/absolute'}
   r = router._HttpRouter(cache, 'https://any.where', dry_run=True)
   self.assertIsInstance(r._http, infra_libs.InstrumentedHttp)
Example #14
0
 def test_push_invalid_event(self):
     r = router._HttpRouter({}, 'https://any.where', dry_run=True)
     self.assertFalse(r.push_event(None))
Example #15
0
 def test_push_invalid_event(self):
   r = router._HttpRouter({}, 'https://any.where', dry_run=True)
   self.assertFalse(r.push_event(None))
Example #16
0
 def test_without_credentials_smoke(self):
     # Use dry_run to avoid code that deals with http.
     r = router._HttpRouter({}, 'https://any.where', dry_run=True)
     self.assertIsInstance(r._http, infra_libs.InstrumentedHttp)
Example #17
0
 def test_without_credentials_smoke(self):
   # Use dry_run to avoid code that deals with http.
   r = router._HttpRouter({}, 'https://any.where', dry_run=True)
   self.assertIsInstance(r._http, infra_libs.InstrumentedHttp)
Example #18
0
def setup_monitoring(run_type='dry',
                     hostname=None,
                     service_name=None,
                     appengine_name=None,
                     output_file=None,
                     dry_run=False,
                     http_timeout=10,
                     http_retry_backoff=2.):
    """Initializes event monitoring.

  This function is mainly used to provide default global values which are
  required for the module to work.

  If you're implementing a command-line tool, use process_argparse_options
  instead.

  Args:
    run_type (str): One of 'dry', 'test', or 'prod'. Do respectively nothing,
      hit the testing endpoint and the production endpoint.

    hostname (str): hostname as it should appear in the event. If not provided
      a default value is computed.

    service_name (str): logical name of the service that emits events. e.g.
      "commit_queue".

    appengine_name (str): name of the appengine app, if running on appengine.

    output_file (str): file where to write the output in run_type == 'file'
      mode.

    dry_run (bool): if True, the code has no side-effect, what would have been
      done is printed instead.

    http_timeout (int): timeout in seconds for HTTP requests to send events.

    http_retry_backoff (float): time in seconds before retrying POSTing to the
         HTTP endpoint. Randomized exponential backoff is applied on subsequent
         retries.
  """
    global _router
    logging.debug('event_mon: setting up monitoring.')

    if _router:
        return

    default_event = ChromeInfraEvent()

    hostname = hostname or socket.getfqdn()
    # hostname might be empty string or None on some systems, who knows.
    if hostname:  # pragma: no branch
        default_event.event_source.host_name = hostname
    else:  # pragma: no cover
        logging.warning('event_mon: unable to determine hostname.')

    if service_name:
        default_event.event_source.service_name = service_name
    if appengine_name:
        default_event.event_source.appengine_name = appengine_name

    _cache['default_event'] = default_event

    if run_type not in RUNTYPES:
        logging.error('Unknown run_type (%s). Setting to "dry"', run_type)
        run_type = 'dry'

    if run_type == 'dry':
        # If we are running on AppEngine or devserver, use logging module.
        server_software = os.environ.get('SERVER_SOFTWARE', '')
        if (server_software.startswith('Google App Engine')
                or server_software.startswith('Development')):
            _router = ev_router._LoggingStreamRouter()
        else:
            _router = ev_router._TextStreamRouter()
    elif run_type == 'file':
        _router = ev_router._LocalFileRouter(output_file, dry_run=dry_run)
    else:
        _router = ev_router._HttpRouter(_cache,
                                        ENDPOINTS.get(run_type),
                                        dry_run=dry_run,
                                        timeout=http_timeout,
                                        retry_backoff=http_retry_backoff)
Example #19
0
def setup_monitoring(run_type='dry',
                     hostname=None,
                     service_name=None,
                     appengine_name=None,
                     service_account_creds=None,
                     service_accounts_creds_root=None,
                     output_file=None,
                     dry_run=False):
  """Initializes event monitoring.

  This function is mainly used to provide default global values which are
  required for the module to work.

  If you're implementing a command-line tool, use process_argparse_options
  instead.

  Args:
    run_type (str): One of 'dry', 'test', or 'prod'. Do respectively nothing,
      hit the testing endpoint and the production endpoint.

    hostname (str): hostname as it should appear in the event. If not provided
      a default value is computed.

    service_name (str): logical name of the service that emits events. e.g.
      "commit_queue".

    appengine_name (str): name of the appengine app, if running on appengine.

    service_account_creds (str): path to a json file containing a service
      account's credentials obtained from a Google Cloud project. **Path is
      relative to service_account_creds_root**, which is not the current path by
      default. See infra_libs.authentication for details.

    service_account_creds_root (str): path containing credentials files.

    output_file (str): file where to write the output in run_type == 'file'
      mode.

    dry_run (bool): if True, the code has no side-effect, what would have been
      done is printed instead.
  """
  global _router
  logging.debug('event_mon: setting up monitoring.')

  if not _router:
    default_event = ChromeInfraEvent()

    hostname = hostname or socket.getfqdn()
    # hostname might be empty string or None on some systems, who knows.
    if hostname:  # pragma: no branch
      default_event.event_source.host_name = hostname
    else:  # pragma: no cover
      logging.warning('event_mon: unable to determine hostname.')

    if service_name:
      default_event.event_source.service_name = service_name
    if appengine_name:
      default_event.event_source.appengine_name = appengine_name

    _cache['default_event'] = default_event
    if run_type in ('prod', 'test'):
      _cache['service_account_creds'] = service_account_creds
      _cache['service_accounts_creds_root'] = service_accounts_creds_root
    else:
      _cache['service_account_creds'] = None
      _cache['service_accounts_creds_root'] = None

    if run_type not in RUNTYPES:
      logging.error('Unknown run_type (%s). Setting to "dry"', run_type)
      run_type = 'dry'

    if run_type == 'dry':
      # If we are running on AppEngine or devserver, use logging module.
      server_software = os.environ.get('SERVER_SOFTWARE', '')
      if (server_software.startswith('Google App Engine') or
          server_software.startswith('Development')):
        _router = ev_router._LoggingStreamRouter()
      else:
        _router = ev_router._TextStreamRouter()
    elif run_type == 'file':
      _router = ev_router._LocalFileRouter(output_file,
                                           dry_run=dry_run)
    else:
      _router = ev_router._HttpRouter(_cache,
                                      ENDPOINTS.get(run_type),
                                      dry_run=dry_run)