1717import random
1818import string
1919
20+ from google .api_core .exceptions import Aborted
2021from google .cloud import monitoring_v3
2122import google .protobuf .json_format
2223import pytest
24+ from retrying import retry
2325
2426import snippets
2527
@@ -29,6 +31,10 @@ def random_name(length):
2931 [random .choice (string .ascii_lowercase ) for i in range (length )])
3032
3133
34+ def retry_if_aborted (exception ):
35+ return isinstance (exception , Aborted )
36+
37+
3238class PochanFixture :
3339 """A test fixture that creates an alert POlicy and a notification CHANnel,
3440 hence the name, pochan.
@@ -42,30 +48,40 @@ def __init__(self):
4248 monitoring_v3 .NotificationChannelServiceClient ())
4349
4450 def __enter__ (self ):
45- # Create a policy.
46- policy = monitoring_v3 .types .alert_pb2 .AlertPolicy ()
47- json = open ('test_alert_policy.json' ).read ()
48- google .protobuf .json_format .Parse (json , policy )
49- policy .display_name = 'snippets-test-' + random_name (10 )
50- self .alert_policy = self .alert_policy_client .create_alert_policy (
51- self .project_name , policy )
52- # Create a notification channel.
53- notification_channel = (
54- monitoring_v3 .types .notification_pb2 .NotificationChannel ())
55- json = open ('test_notification_channel.json' ).read ()
56- google .protobuf .json_format .Parse (json , notification_channel )
57- notification_channel .display_name = 'snippets-test-' + random_name (10 )
58- self .notification_channel = (
59- self .notification_channel_client .create_notification_channel (
60- self .project_name , notification_channel ))
51+ @retry (wait_exponential_multiplier = 1000 , wait_exponential_max = 10000 ,
52+ stop_max_attempt_number = 5 , retry_on_exception = retry_if_aborted )
53+ def setup ():
54+ # Create a policy.
55+ policy = monitoring_v3 .types .alert_pb2 .AlertPolicy ()
56+ json = open ('test_alert_policy.json' ).read ()
57+ google .protobuf .json_format .Parse (json , policy )
58+ policy .display_name = 'snippets-test-' + random_name (10 )
59+ self .alert_policy = self .alert_policy_client .create_alert_policy (
60+ self .project_name , policy )
61+ # Create a notification channel.
62+ notification_channel = (
63+ monitoring_v3 .types .notification_pb2 .NotificationChannel ())
64+ json = open ('test_notification_channel.json' ).read ()
65+ google .protobuf .json_format .Parse (json , notification_channel )
66+ notification_channel .display_name = (
67+ 'snippets-test-' + random_name (10 ))
68+ self .notification_channel = (
69+ self .notification_channel_client .create_notification_channel (
70+ self .project_name , notification_channel ))
71+ setup ()
6172 return self
6273
6374 def __exit__ (self , type , value , traceback ):
6475 # Delete the policy and channel we created.
65- self .alert_policy_client .delete_alert_policy (self .alert_policy .name )
66- if self .notification_channel .name :
67- self .notification_channel_client .delete_notification_channel (
68- self .notification_channel .name )
76+ @retry (wait_exponential_multiplier = 1000 , wait_exponential_max = 10000 ,
77+ stop_max_attempt_number = 5 , retry_on_exception = retry_if_aborted )
78+ def teardown ():
79+ self .alert_policy_client .delete_alert_policy (
80+ self .alert_policy .name )
81+ if self .notification_channel .name :
82+ self .notification_channel_client .delete_notification_channel (
83+ self .notification_channel .name )
84+ teardown ()
6985
7086
7187@pytest .fixture (scope = 'session' )
@@ -81,36 +97,55 @@ def test_list_alert_policies(capsys, pochan):
8197
8298
8399def test_enable_alert_policies (capsys , pochan ):
84- snippets .enable_alert_policies (pochan .project_name , False )
85- out , _ = capsys .readouterr ()
100+ @retry (wait_exponential_multiplier = 1000 , wait_exponential_max = 10000 ,
101+ stop_max_attempt_number = 5 , retry_on_exception = retry_if_aborted )
102+ def invoke_sample (val ):
103+ snippets .enable_alert_policies (pochan .project_name , val )
86104
87- snippets .enable_alert_policies (pochan .project_name , False )
105+ invoke_sample (False )
106+ invoke_sample (False )
88107 out , _ = capsys .readouterr ()
89108 assert "already disabled" in out
90109
91- snippets . enable_alert_policies ( pochan . project_name , True )
110+ invoke_sample ( True )
92111 out , _ = capsys .readouterr ()
93112 assert "Enabled {0}" .format (pochan .project_name ) in out
94113
95- snippets . enable_alert_policies ( pochan . project_name , True )
114+ invoke_sample ( True )
96115 out , _ = capsys .readouterr ()
97116 assert "already enabled" in out
98117
99118
100119def test_replace_channels (capsys , pochan ):
101- alert_policy_id = pochan .alert_policy .name .split ('/' )[- 1 ]
102- notification_channel_id = pochan .notification_channel .name .split ('/' )[- 1 ]
103- snippets .replace_notification_channels (
104- pochan .project_name , alert_policy_id , [notification_channel_id ])
120+ @retry (wait_exponential_multiplier = 1000 , wait_exponential_max = 10000 ,
121+ stop_max_attempt_number = 5 , retry_on_exception = retry_if_aborted )
122+ def invoke_sample ():
123+ alert_policy_id = pochan .alert_policy .name .split ('/' )[- 1 ]
124+ notification_channel_id = pochan .notification_channel .name .split (
125+ '/' )[- 1 ]
126+ snippets .replace_notification_channels (
127+ pochan .project_name , alert_policy_id , [notification_channel_id ])
128+
129+ invoke_sample ()
105130 out , _ = capsys .readouterr ()
106131 assert "Updated {0}" .format (pochan .alert_policy .name ) in out
107132
108133
109134def test_backup_and_restore (capsys , pochan ):
110- snippets .backup (pochan .project_name , 'backup.json' )
135+ @retry (wait_exponential_multiplier = 1000 , wait_exponential_max = 10000 ,
136+ stop_max_attempt_number = 5 , retry_on_exception = retry_if_aborted )
137+ def invoke_backup ():
138+ snippets .backup (pochan .project_name , 'backup.json' )
139+
140+ invoke_backup ()
111141 out , _ = capsys .readouterr ()
112142
113- snippets .restore (pochan .project_name , 'backup.json' )
143+ @retry (wait_exponential_multiplier = 1000 , wait_exponential_max = 10000 ,
144+ stop_max_attempt_number = 5 , retry_on_exception = retry_if_aborted )
145+ def invoke_restore ():
146+ snippets .restore (pochan .project_name , 'backup.json' )
147+
148+ invoke_restore ()
114149 out , _ = capsys .readouterr ()
115150 assert "Updated {0}" .format (pochan .alert_policy .name ) in out
116151 assert "Updating channel {0}" .format (
@@ -119,8 +154,14 @@ def test_backup_and_restore(capsys, pochan):
119154
120155def test_delete_channels (capsys , pochan ):
121156 notification_channel_id = pochan .notification_channel .name .split ('/' )[- 1 ]
122- snippets .delete_notification_channels (
123- pochan .project_name , [notification_channel_id ], force = True )
157+
158+ @retry (wait_exponential_multiplier = 1000 , wait_exponential_max = 10000 ,
159+ stop_max_attempt_number = 5 , retry_on_exception = retry_if_aborted )
160+ def invoke_delete ():
161+ snippets .delete_notification_channels (
162+ pochan .project_name , [notification_channel_id ], force = True )
163+
164+ invoke_delete ()
124165 out , _ = capsys .readouterr ()
125166 assert "{0} deleted" .format (notification_channel_id ) in out
126167 pochan .notification_channel .name = '' # So teardown is not tried
0 commit comments