Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[monitoring] fix: use retrying module in the fixture class #3285

Merged
merged 7 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
1 change: 1 addition & 0 deletions monitoring/api/v3/alerts-client/requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pytest==5.3.2
retrying==1.3.3
43 changes: 27 additions & 16 deletions monitoring/api/v3/alerts-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import random
import string

from google.api_core.exceptions import Aborted
from google.cloud import monitoring_v3
import google.protobuf.json_format
import pytest
from retrying import retry

import snippets

Expand All @@ -29,6 +31,10 @@ def random_name(length):
[random.choice(string.ascii_lowercase) for i in range(length)])


def retry_if_aborted(exception):
return isinstance(exception, Aborted)


class PochanFixture:
"""A test fixture that creates an alert POlicy and a notification CHANnel,
hence the name, pochan.
Expand All @@ -42,22 +48,27 @@ def __init__(self):
monitoring_v3.NotificationChannelServiceClient())

def __enter__(self):
# Create a policy.
policy = monitoring_v3.types.alert_pb2.AlertPolicy()
json = open('test_alert_policy.json').read()
google.protobuf.json_format.Parse(json, policy)
policy.display_name = 'snippets-test-' + random_name(10)
self.alert_policy = self.alert_policy_client.create_alert_policy(
self.project_name, policy)
# Create a notification channel.
notification_channel = (
monitoring_v3.types.notification_pb2.NotificationChannel())
json = open('test_notification_channel.json').read()
google.protobuf.json_format.Parse(json, notification_channel)
notification_channel.display_name = 'snippets-test-' + random_name(10)
self.notification_channel = (
self.notification_channel_client.create_notification_channel(
self.project_name, notification_channel))
@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000,
stop_max_attempt_number=5, retry_on_exception=retry_if_aborted)
def setup():
# Create a policy.
policy = monitoring_v3.types.alert_pb2.AlertPolicy()
json = open('test_alert_policy.json').read()
google.protobuf.json_format.Parse(json, policy)
policy.display_name = 'snippets-test-' + random_name(10)
self.alert_policy = self.alert_policy_client.create_alert_policy(
self.project_name, policy)
# Create a notification channel.
notification_channel = (
monitoring_v3.types.notification_pb2.NotificationChannel())
json = open('test_notification_channel.json').read()
google.protobuf.json_format.Parse(json, notification_channel)
notification_channel.display_name = (
'snippets-test-' + random_name(10))
self.notification_channel = (
self.notification_channel_client.create_notification_channel(
self.project_name, notification_channel))
setup()
return self

def __exit__(self, type, value, traceback):
Expand Down