Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Using a unique class name for each stacktrace.
Also adding a retry until the count comes back as expected.
  • Loading branch information
dhermes committed May 4, 2017
commit 5cee9a8fd8c6d167b78027dc7b189be6b1de55ae
86 changes: 58 additions & 28 deletions error_reporting/tests/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import time
import functools
import operator
import unittest

from google.cloud import error_reporting
Expand All @@ -22,8 +23,12 @@
error_stats_service_pb2)
from google.protobuf.duration_pb2 import Duration

from test_utils.retry import RetryResult
from test_utils.system import unique_resource_id


ERROR_MSG = 'Stackdriver Error Reporting System Test'

ERROR_NAME = 'Stackdriver Error Reporting System Test'

def setUpModule():
Config.CLIENT = error_reporting.Client()
Expand All @@ -38,56 +43,81 @@ class Config(object):
CLIENT = None


def _list_groups(project):
def _list_groups(client):
"""List Error Groups from the last 60 seconds.

This class provides a wrapper around making calls to the GAX
API. It's used by the system tests to find the appropriate error group
to verify the error was successfully reported.

:type project: str
:param project: Google Cloud Project ID
:type client: :class:`~google.cloud.error_reporting.client.Client`
:param client: The client containing a project and credentials.

:rtype: :class:`~google.gax.ResourceIterator`
:returns: Iterable of :class:`~.error_stats_service_pb2.ErrorGroupStats`.
"""
gax_api = error_stats_service_client.ErrorStatsServiceClient(
credentials=Config.CLIENT._credentials)
project_name = gax_api.project_path(project)
credentials=client._credentials)
project_name = gax_api.project_path(client.project)

time_range = error_stats_service_pb2.QueryTimeRange()
time_range.period = error_stats_service_pb2.QueryTimeRange.PERIOD_1_HOUR

duration = Duration(seconds=60*60)
duration = Duration(seconds=60 * 60)

return gax_api.list_group_stats(
project_name, time_range, timed_count_duration=duration)


def _simulate_exception():
"""Simulates an exception to verify it was reported."""
def _simulate_exception(class_name, client):
"""Simulates an exception to verify it was reported.

:type class_name: str
:param class_name: The name of a custom error class to
create (and raise).

:type client: :class:`~google.cloud.error_reporting.client.Client`
:param client: The client that will report the exception.
"""
custom_exc = type(class_name, (RuntimeError,), {})
try:
raise RuntimeError(ERROR_NAME)
raise custom_exc(ERROR_MSG)
except RuntimeError:
Config.CLIENT.report_exception()
client.report_exception()


class TestErrorReporting(unittest.TestCase):
def _get_error_count(class_name, client):
"""Counts the number of errors in the group of the test exception.

def _get_error_count(self):
"""Counts the number of errors in the group of the test exception."""
groups = _list_groups(Config.CLIENT.project)
for group in groups:
if ERROR_NAME in group.representative.message:
return group.count
:type class_name: str
:param class_name: The name of a custom error class used.

:type client: :class:`~google.cloud.error_reporting.client.Client`
:param client: The client containing a project and credentials.

:rtype: int
:returns: Group count for errors that match ``class_name``. If no
match is found, returns :data:`None`.
"""
groups = _list_groups(client)
for group in groups:
if class_name in group.representative.message:
return group.count


class TestErrorReporting(unittest.TestCase):

def test_report_exception(self):
# If test has never run, group won't exist until we report first
# exception, so first simulate it just to create the group
_simulate_exception()
time.sleep(2)
# Get a class name unique to this test case.
class_name = 'RuntimeError' + unique_resource_id('_')

initial_count = self._get_error_count()
_simulate_exception()
# Simulate an error: group won't exist until we report
# first exception.
_simulate_exception(class_name, Config.CLIENT)

time.sleep(2)
new_count = self._get_error_count()
is_one = functools.partial(operator.eq, 1)
is_one.__name__ = 'is_one' # partial() has no name.
wrapped_get_count = RetryResult(is_one)(_get_error_count)

self.assertEqual(new_count, initial_count + 1)
error_count = wrapped_get_count(class_name, Config.CLIENT)
self.assertEqual(error_count, 1)