Ejemplo n.º 1
0
 def test_stdout_smoke(self):
     event = LogRequestLite.LogEventLite()
     event.event_time_ms = router.time_ms()
     event.event_code = 1
     event.event_flow_id = 2
     r = router._TextStreamRouter()
     self.assertTrue(r.push_event(event))
Ejemplo n.º 2
0
 def test_stdout_smoke(self):
   event = LogRequestLite.LogEventLite()
   event.event_time_ms = router.time_ms()
   event.event_code = 1
   event.event_flow_id = 2
   r = router._TextStreamRouter()
   self.assertTrue(r.push_event(event))
Ejemplo n.º 3
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))
Ejemplo n.º 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))
Ejemplo n.º 5
0
 def test_events_are_logged_with_specified_severity(self, log_mock):
   logger = router._LoggingStreamRouter(logging.WARN)
   event = LogRequestLite.LogEventLite()
   event.event_time_ms = router.time_ms()
   event.event_code = 1
   event.event_flow_id = 2
   self.assertTrue(logger.push_event(event))
   self.assertEqual(log_mock.call_args[0], (logging.WARN, mock.ANY))
Ejemplo n.º 6
0
 def test_events_are_logged_with_specified_severity(self, log_mock):
     logger = router._LoggingStreamRouter(logging.WARN)
     event = LogRequestLite.LogEventLite()
     event.event_time_ms = router.time_ms()
     event.event_code = 1
     event.event_flow_id = 2
     self.assertTrue(logger.push_event(event))
     self.assertEqual(log_mock.call_args[0], (logging.WARN, mock.ANY))
Ejemplo n.º 7
0
 def test_non_existing_directory(self):
   req = LogRequestLite.LogEventLite()
   req.event_time_ms = router.time_ms()
   req.event_code = 1
   req.event_flow_id = 2
   r = router._LocalFileRouter(
     os.path.join('non_existing_d1r_31401789', 'output.db'), dry_run=False)
   self.assertFalse(r.push_event(req))
Ejemplo n.º 8
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))
Ejemplo n.º 9
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))
Ejemplo n.º 10
0
 def test_fails_to_log(self, exception_mock):
   logger = router._LoggingStreamRouter(logging.WARN)
   event = LogRequestLite.LogEventLite()
   event.event_time_ms = router.time_ms()
   event.event_code = 1
   event.event_flow_id = 2
   event.source_extension = 'not-a-message'
   self.assertFalse(logger.push_event(event))
   self.assertEqual(exception_mock.call_args[0], ('Unable to log the events',))
Ejemplo n.º 11
0
 def test_closed_stream(self):
     event = LogRequestLite.LogEventLite()
     event.event_time_ms = router.time_ms()
     event.event_code = 1
     event.event_flow_id = 2
     stream = StringIO.StringIO()
     stream.close()
     r = router._TextStreamRouter(stream=stream)
     self.assertFalse(r.push_event(event))
Ejemplo n.º 12
0
 def test_non_existing_directory(self):
     req = LogRequestLite.LogEventLite()
     req.event_time_ms = router.time_ms()
     req.event_code = 1
     req.event_flow_id = 2
     r = router._LocalFileRouter(os.path.join('non_existing_d1r_31401789',
                                              'output.db'),
                                 dry_run=False)
     self.assertFalse(r.push_event(req))
Ejemplo n.º 13
0
  def test_push_smoke(self):
    r = router._Router({}, endpoint=None)

    req = LogRequestLite.LogEventLite()
    req.event_time_ms = router.time_ms()
    req.event_code = 1
    req.event_flow_id = 2
    r.push_event(req)
    self.assertTrue(r.close())
Ejemplo n.º 14
0
 def test_closed_stream(self):
   event = LogRequestLite.LogEventLite()
   event.event_time_ms = router.time_ms()
   event.event_code = 1
   event.event_flow_id = 2
   stream = StringIO.StringIO()
   stream.close()
   r = router._TextStreamRouter(stream=stream)
   self.assertFalse(r.push_event(event))
Ejemplo n.º 15
0
 def test_fails_to_log(self, exception_mock):
     logger = router._LoggingStreamRouter(logging.WARN)
     event = LogRequestLite.LogEventLite()
     event.event_time_ms = router.time_ms()
     event.event_code = 1
     event.event_flow_id = 2
     event.source_extension = 'not-a-message'
     self.assertFalse(logger.push_event(event))
     self.assertEqual(exception_mock.call_args[0],
                      ('Unable to log the events', ))
Ejemplo n.º 16
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)
Ejemplo n.º 17
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)
Ejemplo n.º 18
0
  def test_existing_directory_dry_run(self):
    event = LogRequestLite.LogEventLite()
    event.event_time_ms = router.time_ms()
    event.event_code = 1
    event.event_flow_id = 2

    with infra_libs.temporary_directory(prefix='event-mon-') as tempdir:
      filename = os.path.join(tempdir, 'output.db')
      r = router._LocalFileRouter(filename, dry_run=True)
      self.assertTrue(r.push_event(event))

      # Check that the file has not been written
      self.assertFalse(os.path.exists(filename))
Ejemplo n.º 19
0
    def test_existing_directory_dry_run(self):
        event = LogRequestLite.LogEventLite()
        event.event_time_ms = router.time_ms()
        event.event_code = 1
        event.event_flow_id = 2

        with infra_libs.temporary_directory(prefix='event-mon-') as tempdir:
            filename = os.path.join(tempdir, 'output.db')
            r = router._LocalFileRouter(filename, dry_run=True)
            self.assertTrue(r.push_event(event))

            # Check that the file has not been written
            self.assertFalse(os.path.exists(filename))
Ejemplo n.º 20
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)
Ejemplo n.º 21
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)
Ejemplo n.º 22
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)
Ejemplo n.º 23
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)
Ejemplo n.º 24
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)
Ejemplo n.º 25
0
  def test_existing_directory(self):
    event = LogRequestLite.LogEventLite()
    event.event_time_ms = router.time_ms()
    event.event_code = 1
    event.event_flow_id = 2

    with infra_libs.temporary_directory(prefix='event-mon-') as tempdir:
      filename = os.path.join(tempdir, 'output.db')
      r = router._LocalFileRouter(filename, dry_run=False)
      self.assertTrue(r.push_event(event))

      # Check that the file is readable and contains the right data.
      with open(filename, 'rb') as f:
        req_read = LogRequestLite.FromString(f.read())
        self.assertEqual(len(req_read.log_event), 1)
        event_read = req_read.log_event[0]
        self.assertEqual(event_read.event_time_ms, event.event_time_ms)
        self.assertEqual(event_read.event_code, event.event_code)
        self.assertEqual(event_read.event_flow_id, event.event_flow_id)
Ejemplo n.º 26
0
    def test_existing_directory(self):
        event = LogRequestLite.LogEventLite()
        event.event_time_ms = router.time_ms()
        event.event_code = 1
        event.event_flow_id = 2

        with infra_libs.temporary_directory(prefix='event-mon-') as tempdir:
            filename = os.path.join(tempdir, 'output.db')
            r = router._LocalFileRouter(filename, dry_run=False)
            self.assertTrue(r.push_event(event))

            # Check that the file is readable and contains the right data.
            with open(filename, 'rb') as f:
                req_read = LogRequestLite.FromString(f.read())
                self.assertEqual(len(req_read.log_event), 1)
                event_read = req_read.log_event[0]
                self.assertEqual(event_read.event_time_ms, event.event_time_ms)
                self.assertEqual(event_read.event_code, event.event_code)
                self.assertEqual(event_read.event_flow_id, event.event_flow_id)
Ejemplo n.º 27
0
def _get_log_event_lite(chrome_infra_event, event_timestamp=None):
  """Wraps a ChromeInfraEvent into a LogEventLite.

  Args:
    event_timestamp (int or float): timestamp of when the event happened
      as a number of milliseconds since the epoch. If None, the current time
      is used.

  Returns:
    log_event (log_request_lite_pb2.LogRequestLite.LogEventLite):
  """
  if not isinstance(event_timestamp, (int, float, None.__class__ )):
    logging.error('Invalid type for event_timestamp. Needs a number, got %s',
                  type(event_timestamp))
    return None

  log_event = LogRequestLite.LogEventLite()
  log_event.event_time_ms = int(event_timestamp or router.time_ms())
  log_event.source_extension = chrome_infra_event.SerializeToString()
  return log_event
Ejemplo n.º 28
0
def _get_log_event_lite(chrome_infra_event, event_timestamp=None):
  """Wraps a ChromeInfraEvent into a LogEventLite.

  Args:
    event_timestamp (int or float): timestamp of when the event happened
      as a number of milliseconds since the epoch. If None, the current time
      is used.

  Returns:
    log_event (log_request_lite_pb2.LogRequestLite.LogEventLite):
  """
  if not isinstance(event_timestamp, (int, float, None.__class__ )):
    logging.error('Invalid type for event_timestamp. Needs a number, got %s',
                  type(event_timestamp))
    return None

  log_event = LogRequestLite.LogEventLite()
  log_event.event_time_ms = int(event_timestamp or router.time_ms())
  log_event.source_extension = chrome_infra_event.SerializeToString()
  return log_event
Ejemplo n.º 29
0
  def test_events_are_logged_correctly(self, log_mock):
    logger = router._LoggingStreamRouter()
    events = []
    for i in range(3):
      event = LogRequestLite.LogEventLite()
      event.event_time_ms = router.time_ms()
      event.event_code = 1
      event.event_flow_id = 2
      infra_event = ChromeInfraEvent()
      infra_event.cq_event.issue = str(i + 1)
      event.source_extension = infra_event.SerializeToString()
      events.append(event)

    self.assertTrue(logger.push_event(events))

    expected_calls = [
        ((logging.INFO,
          'Sending event_mon event:\ncq_event {\n  issue: "1"\n}\n'),),
        ((logging.INFO,
          'Sending event_mon event:\ncq_event {\n  issue: "2"\n}\n'),),
        ((logging.INFO,
          'Sending event_mon event:\ncq_event {\n  issue: "3"\n}\n'),)]
    self.assertEqual(log_mock.call_args_list, expected_calls)
Ejemplo n.º 30
0
    def test_events_are_logged_correctly(self, log_mock):
        logger = router._LoggingStreamRouter()
        events = []
        for i in range(3):
            event = LogRequestLite.LogEventLite()
            event.event_time_ms = router.time_ms()
            event.event_code = 1
            event.event_flow_id = 2
            infra_event = ChromeInfraEvent()
            infra_event.cq_event.issue = str(i + 1)
            event.source_extension = infra_event.SerializeToString()
            events.append(event)

        self.assertTrue(logger.push_event(events))

        expected_calls = [
            ((logging.INFO,
              'Sending event_mon event:\ncq_event {\n  issue: "1"\n}\n'), ),
            ((logging.INFO,
              'Sending event_mon event:\ncq_event {\n  issue: "2"\n}\n'), ),
            ((logging.INFO,
              'Sending event_mon event:\ncq_event {\n  issue: "3"\n}\n'), )
        ]
        self.assertEqual(log_mock.call_args_list, expected_calls)